Jan-21-2021, 04:43 PM
Don't modify an iterator while you're iterating over it. Some options are to loop over a copy of the list or to just copy the data you do want rather than delete the data you don't.
l = [1, 2, 3, 2, 2, 2, 1] for el in l[:]: # loops over a copy of l. Modifying l doesn't change the loop iterator. ...
l = [1, 2, 3, 2, 2, 2, 1] l = [e for e in if l != 2] # list comprehension copies the data we want directly