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]

-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to