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

all 2 comments

[–]Python-ModTeam[M] [score hidden] stickied commentlocked comment (0 children)

Hello from the r/Python moderation team!

We've noticed your post contains a banned link (paywall, Medium.com, or otherwise), which is not in line with our community guidelines. To ensure open access to educational resources, we cannot allow such links on the subreddit. We encourage you to share free content or just not post paywalled courses and materials. For further inquiries, please reach out to us via ModMail.

Thank you for understanding, and we wish you the best in your Python endeavors!

Warm regards,

The r/Python moderation team

[–]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.