how to break a for loop?

Schüle Daniel uval at rz.uni-karlsruhe.de
Mon Feb 20 21:05:12 EST 2006


Gregory Petrosyan wrote:
> Hello!
> It's 1:56 o'clock in St.-Petersburg now, and I am still coding... maybe
> that's why I encountered stupid problem: I need to remove zeros from
> the begining of list, but I can't :-(. I use
> 
>         for i,coef in enumerate(coefs):
>             if coef == 0:
>                 del coefs[i]
>             else:
>                 break
> 
> but it removes ALL zeros from list. What's up?

I don't know how enumerate is implemented, but I would
suspect that modifying the list object in the loop
through del is asking for trouble

try
for i,coef in enumerate(coefs[:]):
instead

> P.S. I feel SO stupid asking this quastion... ;-)

uda4i

hth, Daniel




More information about the Python-list mailing list