For now i've been trying to perform strip() on a list of strings and i did this:
i = 0
for j in alist:
alist[i] = j.strip()
i+=1
Is there a better way of doing that?
For now i've been trying to perform strip() on a list of strings and i did this:
i = 0
for j in alist:
alist[i] = j.strip()
i+=1
Is there a better way of doing that?
You probably shouldn't be using list
as a variable name since it's a type. Regardless:
list = map(str.strip, list)
This will apply the function str.strip
to every element in list
, return a new list, and store the result back in list
.
You could use list comprehensions
stripped_list = [j.strip() for j in initial_list]
Some intriguing discussions on performance happened here, so let me provide a benchmark:
http://ideone.com/ldId8
noslice_map : 0.0814900398254
slice_map : 0.084676027298
noslice_comprehension : 0.0927240848541
slice_comprehension : 0.124806165695
iter_manual : 0.133514881134
iter_enumerate : 0.142778873444
iter_range : 0.160353899002
So:
map(str.strip, my_list)
is the fastest way, it's just a little bit faster than comperhensions.
map
or itertools.imap
if there's a single function that you want to apply (like str.split)my_list[:] = map...
, the slice notation introduces only a small overhead and is likely to spare you some bugs if there are multiple references to that list.
I think you mean
a_list = [s.strip() for s in a_list]
Using a generator expression may be a better approach, like this:
stripped_list = (s.strip() for s in a_list)
offers the benefit of lazy evaluation, so the strip
only runs when the given element, stripped, is needed.
If you need references to the list to remain intact outside the current scope, you might want to use list slice syntax.:
a_list[:] = [s.strip() for s in a_list]
For commenters interested in the speed of various approaches, it looks as if in CPython the generator-to-slice approach is the least efficient:
>>> from timeit import timeit as t
>>> t("""a[:]=(s.strip() for s in a)""", """a=[" %d " % s for s in range(10)]""")
4.35184121131897
>>> t("""a[:]=[s.strip() for s in a]""", """a=[" %d " % s for s in range(10)]""")
2.9129951000213623
>>> t("""a=[s.strip() for s in a]""", """a=[" %d " % s for s in range(10)]""")
2.47947096824646