how to break a for loop?

bonono at gmail.com bonono at gmail.com
Tue Feb 21 03:46:46 EST 2006


Giovanni Bajo wrote:
> bonono at gmail.com wrote:
>
> >> I need to remove zeros from
> >> the begining of list, but I can't :-(.
> >
> > I believe the following is almost a direct translation of the above
> > sentence.
> >
> > import itertools as it
> > a=[0,0,0,1,0]
> > a[:]=it.dropwhile(lambda x: x is 0, a)
>
>
> Usa lambda x: x==0 or simply lambda x: x
>
> Using 'is' relies on the interpreter reusing existing instances of the
> immutable object '0', which is an implementation detail.

stand corrected. But I don't think "lambda x: x" is I want as 0 would
be false which would stop the dropwhile right away.

For this particular case one can even use operator.not_ directly, which
skip the lambda(has some peformance advantage).




More information about the Python-list mailing list