split string by arbitrary number of white spaces

python, split

Solution

Just use `my_str.split()` without `' '`.

More, you can also indicate how many splits to perform by specifying the second parameter:

>>> ' 1 2 3 4  '.split(None, 2)
['1', '2', '3 4  ']
>>> ' 1 2 3 4  '.split(None, 1)
['1', '2 3 4  ']

Problem

I'm trying to find the most pythonic way to split a string like "some words in a string" into single words. `string.split(' ')` works ok but it returns a bunch of white space entries in the list. Of course i could iterate the list and remove the white spaces but I was wondering if there was a better way?

Original source

Related problems