list traversal and remove

Arnaud Delobelle arnodel at googlemail.com
Thu Jan 31 03:56:10 EST 2008


On Jan 31, 7:49 am, "digisat... at gmail.com" <digisat... at gmail.com>
wrote:
> I supposed the below code will print seven 2 and generate the list li
> without 2.
> Strangely it only print  four 2. If you change the number of 2 in the
> list, the results are all beyond expectation.
> I know the other way to achieve the expected goal, but why this is
> happening? Could somebody enlight me?
>
> li= [2,2,2,2,2,2,2,3,4,5,6,7,8,9]
> for x in li:
>     if x == 2:
>         print x
>         li.remove(x)

There's just been a thread on this:

http://groups.google.com/group/comp.lang.python/browse_thread/thread/290623baaa46e72a/

--
Arnaud



More information about the Python-list mailing list