Split by comma and strip whitespace in Python Split by comma and strip whitespace in Python python python

Split by comma and strip whitespace in Python


Use list comprehension -- simpler, and just as easy to read as a for loop.

my_string = "blah, lots  ,  of ,  spaces, here "result = [x.strip() for x in my_string.split(',')]# result is ["blah", "lots", "of", "spaces", "here"]

See: Python docs on List Comprehension
A good 2 second explanation of list comprehension.


I came to add:

map(str.strip, string.split(','))

but saw it had already been mentioned by Jason Orendorff in a comment.

Reading Glenn Maynard's comment in the same answer suggesting list comprehensions over map I started to wonder why. I assumed he meant for performance reasons, but of course he might have meant for stylistic reasons, or something else (Glenn?).

So a quick (possibly flawed?) test on my box applying the three methods in a loop revealed:

[word.strip() for word in string.split(',')]$ time ./list_comprehension.py real    0m22.876smap(lambda s: s.strip(), string.split(','))$ time ./map_with_lambda.py real    0m25.736smap(str.strip, string.split(','))$ time ./map_with_str.strip.py real    0m19.428s

making map(str.strip, string.split(',')) the winner, although it seems they are all in the same ballpark.

Certainly though map (with or without a lambda) should not necessarily be ruled out for performance reasons, and for me it is at least as clear as a list comprehension.

Edit:

Python 2.6.5 on Ubuntu 10.04


Split using a regular expression. Note I made the case more general with leading spaces. The list comprehension is to remove the null strings at the front and back.

>>> import re>>> string = "  blah, lots  ,  of ,  spaces, here ">>> pattern = re.compile("^\s+|\s*,\s*|\s+$")>>> print([x for x in pattern.split(string) if x])['blah', 'lots', 'of', 'spaces', 'here']

This works even if ^\s+ doesn't match:

>>> string = "foo,   bar  ">>> print([x for x in pattern.split(string) if x])['foo', 'bar']>>>

Here's why you need ^\s+:

>>> pattern = re.compile("\s*,\s*|\s+$")>>> print([x for x in pattern.split(string) if x])['  blah', 'lots', 'of', 'spaces', 'here']

See the leading spaces in blah?

Clarification: above uses the Python 3 interpreter, but results are the same in Python 2.