newby question: Splitting a string - separator

Tim Peters tim.peters at gmail.com
Fri Dec 9 21:42:55 EST 2005


[James Stroud]
>> The one I like best goes like this:
>>
>> py> data = "Guido van Rossum  Tim Peters     Thomas Liesner"
>> py> names = [n for n in data.split() if n]
>> py> names
>> ['Guido', 'van', 'Rossum', 'Tim', 'Peters', 'Thomas', 'Liesner']
>>
>> I think it is theoretically faster (and more pythonic) than using regexes.

[Kent Johnson]
> Unfortunately it gives the wrong result.

Still, it gets extra points for being such a pleasing example ;-)



More information about the Python-list mailing list