... remove all 0 values

Simon Forman sajmikins at gmail.com
Wed Jul 8 14:01:28 EDT 2009


On Jul 8, 10:44 am, Daniel Austria <futureb... at gmx.at> 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.  list.remove() will
> only remove the first occurence. Doing that while no exception is
> raised is also uncool, right?
>
> Some suggestions?
>
> Best,
> Dan

If you are doing something like this:

L = [0, 0, 0, 1, 1, 1, 0]
removeZeros(L)
number_of_ones = len(L)

you can just use sum() like so:

number_of_ones = sum(L)



HTH



More information about the Python-list mailing list