[0, 0, 0, 1, 1, 1, 0] ... remove all 0 values

Paul Rubin http
Wed Jul 8 14:49:52 EDT 2009


Daniel Austria <futurebase at gmx.at> writes:
> just one question. How can i remove all 0 values in a list? 

I prefer:

   newlist = list(x for x in oldlist if x != 0)

to the square bracket list comprehension that a few people have
suggested.  This is because in python 2.x, the listcomp "leaks" its
index variable into the surrounding scope, but the generator
expression above doesn't.  Usually not a big deal, but an extra bit of
hygiene never(?) hurts.



More information about the Python-list mailing list