strange list.remove behaviour in loops

Terry Reedy tjreedy at udel.edu
Sun Dec 29 23:52:48 EST 2002


"peter" <peter at nobody.de> wrote in message
news:95gv0vkn4af311fidufibu9dd1q67mn7q8 at 4ax.com...

> Python 2.2.1 (#1, Jul 29 2002, 23:15:49)
> >>> A = [1,2,3,4,5,6,7,8,9]
> >>> for a in A:
> ...     print a
> ...     A.remove(a)
> ...
> 1
> 3
> 5
> 7
> 9
>
> instead of removing the actual item from the list it removes the
next
> one. is this by design? if yes how can i work around it. i don't
> really want to clone the list and remove from the clonelist instead.

Quote from the reference manual:
"Warning: There is a subtlety when the sequence is being modified by
the loop (this can only occur for mutable sequences, i.e. lists). An
internal counter is used to keep track of which item is used next, and
this is incremented on each iteration. When this counter has reached
the length of the sequence the loop terminates. This means that if the
suite deletes the current (or a previous) item from the sequence, the
next item will be skipped (since it gets the index of the current item
which has already been treated). Likewise, if the suite inserts an
item in the sequence before the current item, the current item will be
treated again the next time through the loop. This can lead to nasty
bugs that can be avoided by making a temporary copy using a slice of
the whole sequence, e.g.,


for x in a[:]:
    if x < 0: a.remove(x)
"

The other fix (also more efficient) is to loop backwards with range or
pop.

>>> l=range(10)
>>> while l:
...   print l.pop()
...
9
8
7
6
5
4
3
2
1
0

Terry J. Reedy





More information about the Python-list mailing list