Remove empty strings from list

Gabriel Genellina gagsl-py2 at
Tue Sep 15 05:52:47 CEST 2009

En Mon, 14 Sep 2009 23:33:05 -0300, tec <technic.tec at> escribió:

> or use filter
> list=filter(lambda x: len(x)>0, list)

For strings, len(x)>0 <=> len(x) <=> x, so the above statement is  
equivalent to:

list=filter(lambda x: x, list)

which according to the documentation is the same as:

list=filter(None, list)

which is the fastest variant AFAIK.

(Of course, it's even better to use the right split() call so there is no  
empty strings to filter out in the first place)

Gabriel Genellina

More information about the Python-list mailing list