Problem splitting a string

Alex Martelli aleax at mail.comcast.net
Sat Oct 15 04:54:56 EDT 2005


Mike Meyer <mwm at mired.org> wrote:
   ...
> A third alternative is to split once, then split the substrings a
> second time and stitch the results back together:
> 
> >>> sum([x.split('_') for x in mystr.split()], [])
> ['this', 'NP', 'is', 'VL', 'funny', 'JJ']
> 
> Which is probably slow. To bad extend doesn't take multiple arguments.

Using sum on lists is DEFINITELY slow -- avoid it like the plague.

If you have a list of lists LOL, DON'T use sum(LOL, []), but rather

[x for x in y for y in LOL]


Alex



More information about the Python-list mailing list