Fwd: PYTHON BUG. deleting elements of list.
Mats Wichmann
mats at wichmann.us
Tue Sep 8 19:06:48 EDT 2020
On 9/7/20 5:01 PM, Driuma Nikita wrote:
_list = list(range(50))
for i, el in enumerate(_list):
del _list[i]
print(_list)
Don't change the the list while you are iterating over it, it messes up
the iteration. It's not "randomly deleting", it's when next is called to
fetch the next item, the list has changed.
One workaround is to iterate over a copy. For example here's using
slicing to create a new list to iterate over:
for i, el in enumerate(_list[:]):
del _list[i]
More information about the Python-list
mailing list