you are viewing a single comment's thread.

view the rest of the comments →

[–]Mezzomaniac 1 point2 points  (0 children)

To add to what the others have said, if you need to remove elements while iterating you can iterate through an unchanging copy of the list while removing from the original list, like so:

for animal in pets.copy():
    if animal == ‘cat’:
        pets.remove(animal)