Better solution
Mark McEahern
marklists at mceahern.com
Tue Aug 20 11:03:02 EDT 2002
> Well, I want to throw away a _same_ garbage from a list with less
> of coding.
> This is current code, sure not the best ;-) Is any better solutions?
>
> --------------8<------------------------
> lst = ['', 'a', '', 'b', 'c', '', 'd']
> map(lambda z:lst.pop(lst.index('')), range(0, lst.count('')))
> --------------8<------------------------
>
> Now lst equals to ['a', 'b', 'c', 'd'].
These would also do the trick and be less cryptic:
filter(lambda x: x, lst)
[x for x in lst if x]
Cheers,
// m
-
More information about the Python-list
mailing list