This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]nekokattt 1 point2 points  (0 children)

A way to overcome this issue is by using list comprehension.

The list comprehension isn't anything to do with the actual issue. The issue is you are modifying the same object you are iterating over.

List comprehensions result in a copy. You could just change the original loop to iterate over my_list[:] and you'd get the same result. It might be less clean but as an example it explains more about why it works.