Removal of element from list while traversing causes the next element to be skipped

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Thu Jan 31 19:25:42 EST 2008


En Thu, 31 Jan 2008 15:45:42 -0200, <Matthew_WARREN at bnpparibas.com>  
escribió:

> Hmm, how does this fare??
>
> for i in range(len(a)):
>       if a[i]==99: a=a[:i]+a[i+1:]
>
>
> I like following your guys code noodling.  I can come up with something
> that does what it appears your doing, sometimes, but as to it's relevant
> merits I havent a clue :)

It's worse than the original `del a[i]`; not only skips over some  
elements, but you'll get an IndexError at the end

-- 
Gabriel Genellina




More information about the Python-list mailing list