Python: Removing list element while iterating over list [duplicate]

You could always iterate over a copy of the list, leaving you free to modify the original:

for item in list(somelist):
  ...
  somelist.remove(item)

Leave a Comment