This is a useful bit of code for removing whitespace from a list of strings. For example, if you split a string, there might be superfluous whitespace.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | # striplist.py
# Take a list of string objects and return the same list
# stripped of extra whitespace.
def striplist(l):
return([x.strip() for x in l])
# This may look a bit more elegant, but it is significantly slower than
# striplist(). This may be dur to the fact that it's using the string.strip()
# method.
from string import strip
def inefficient(l):
return(map(strip, l))
del strip
# Another version of inefficient() using builtin strip()
def inefficient_as_well(l):
return(map(lambda x: x.strip(), l))
# This is only slightly slower than or comparable to striplist()
def comparable(l)
for x in xrange(len(l)):
l[x] = l[x].strip()
return(l)
# This is also on the same order as both comparabale() & striplist()
def comparable_as_well(l)
tmp = []
for x in len(l):
tmp.append(x.strip())
return(tmp)
# An example of a class that would have the strip method. It inherits
# everything from "list", and adds a method, .strip()
# This is slower than striplist()
class StrippableList(list):
def __init__(self,l=[]):
list.__init__(self,l)
def strip(self,char=None):
return(StrippableList([x.strip(char) for x in self]))
|
This is a function that I use quite a bit, and so far is the fastest implementation I could come up with. Since Python has a high overhead for function calls, the most efficient way is to strip the elements you need from within your code. In fact, it may be faster to implement the [x.strip() for x in l] idiom right in your code.
My reason for getting into this was because I was having a huge problem with the speed of some of my code. Using profile showed me that the function calls I had going out to strip the list of strings was having a big impact on the speed of execution. Simple functions can have a big impact!
An alternative might be to add a .strip() method to the list class -- this might improve speed (by pushing the code into C). The class StrippableList() is an example in python (since I'm no C coder). It's slower than striplist(), however.