a bug in list.remove?

Dieter Maurer dieter at handshake.de
Mon Aug 21 13:37:48 EDT 2006


Astan Chee <stanc at al.com.au> writes on Sat, 19 Aug 2006 03:34:26 +1000:
>  >>> for p in ps:
>     if p in qs:
>         ps.remove(p)

You are modifying an object ("ps") while you iterate over it.
This is a receipe for surprises...

The standard idiom is to iterate over a copy rather than the object itself:

        for p in ps[:]:
          ....


Dieter



More information about the Python-list mailing list