8

I would like to split a String by comma ',' and remove whitespace from the beginning and end of each split.

For example, if I have the string:

"QVOD, Baidu Player"

I would like to split and strip to:

['QVOD', 'Baidu Player']

Is there an elegant way of doing this? Possibly using a list comprehension?

3
  • Will the format of the data be the same, always? Commented Jan 21, 2014 at 14:47
  • 1
    Possible duplicate of this question. Not completely, but you can find all the answers you need there. Commented Jan 21, 2014 at 14:50
  • @Kraay89 It is not a duplicate of the question you link to. Commented Feb 20, 2014 at 8:17

2 Answers 2

13

Python has a spectacular function called split that will keep you from having to use a regex or something similar. You can split your string by just calling my_string.split(delimiter)

After that python has a strip function which will remove all whitespace from the beginning and end of a string.

[item.strip() for item in my_string.split(',')]

Benchmarks for the two methods are below:

>>> import timeit
>>> timeit.timeit('map(str.strip, "QVOD, Baidu Player".split(","))', number=100000)
0.3525350093841553
>>> timeit.timeit('map(stripper, "QVOD, Baidu Player".split(","))','stripper=str.strip', number=100000)
0.31575989723205566
>>> timeit.timeit("[item.strip() for item in 'QVOD, Baidu Player'.split(',')]", number=100000)
0.246596097946167

So the list comp is about 33% faster than the map.

Probably also worth noting that as far as being "pythonic" goes, Guido himself votes for the LC. http://www.artima.com/weblogs/viewpost.jsp?thread=98196

Sign up to request clarification or add additional context in comments.

8 Comments

@DSM Whoops, my bad. Thanks for catching that.
Thanks I tried something like this already and each part QVOD ect.. is placed on a new line. The answer below works well as everything is in a list.
@DanielPilch: both methods produce the same list.
@SlaterTyranus Please check the updated code. This might perform almost as fast as LC.
@thefourtheye It's faster than the other version, but still noticeably slower than the LC
|
4

A little functional approach. split function, splits the string based on , and the each and every element will be stripped by str.strip, by map.

>>> stripper = str.strip
>>> map(stripper, "QVOD, Baidu Player".split(","))
['QVOD', 'Baidu Player']

Little timing comparison

import timeit
stripper = str.strip
print timeit.timeit('map(stripper, "QVOD, Baidu Player".split(","))', "from __main__ import stripper", number=100000)
print timeit.timeit("[item.strip() for item in 'QVOD, Baidu Player'.split(',')]", number=100000)

Output on my machine

0.553178071976
0.569463968277

So, both the List comprehension method and map method perform almost the same.

4 Comments

Why are you assigning str.strip to a variable? Why not pass it directly -map(str.strip, "QVOD, Baidu Player".split(","))
@pyrospade That doesn't have to got from str everytime. So, little performance gain :)
@thefourtheye Really? I would imagine that when you pass str.strip to map(function, sequence) it is getting assigned to the function parameter and used locally as a variable there.
@thefourtheye What version of python are you running?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.