[issue41892] use both "for in" and "ElementTree.remove" has a index bug

Serhiy Storchaka report at bugs.python.org
Wed Sep 30 10:43:11 EDT 2020


Serhiy Storchaka <storchaka+cpython at gmail.com> added the comment:

You will get the same behavior for lists:

>>> a = [1, 2, 3]
>>> for x in a:
...     if x == 1:
...         a.remove(x)
...     print(x)
... 
1
3

Lists are iterated by index. First you get an item at index 0, then at index 1, etc, to the end of the list. Initially the list is [1, 2, 3]. After removing 1 at the first iteration it becomes [2, 3], and at the next iteration you get an item at index 1 which is 3.

----------
nosy: +serhiy.storchaka

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue41892>
_______________________________________


More information about the Python-bugs-list mailing list