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

Diez B. Roggisch deets at nospam.web.de
Wed Jul 8 11:06:23 EDT 2009


Daniel Austria wrote:

> Hi python - hackers,
> 
> just one question. How can i remove all 0 values in a list? Sure - i
> can loop over it, but that s not a neat style.

Why not? If you need to potentially look at *all* elements of a list,
nothing but a loop will take you there.

OTOH, your proposed "remove until nothing is found"-thingy will become
quadratic in behavior, as remove also loops over the list - so if you have
list with say 10 ones followed by 10 zeros, you will loop ten times for 11
elements, which is in the order of (n/2)**2.

Diez



More information about the Python-list mailing list