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?

Slater Victoroff
  • 21,376
  • 21
  • 85
  • 144
Daniel Pilch
  • 2,097
  • 5
  • 24
  • 30

2 Answers2

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

Slater Victoroff
  • 21,376
  • 21
  • 85
  • 144
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.

thefourtheye
  • 233,700
  • 52
  • 457
  • 497