I think a little illustration is in order to show the folly of removing an element from a list while iterating over it.
In [1]: l = list(range(10)) In [2]: for elem in l: ...: l.remove(elem) ...: print(elem) ...: 0 2 4 6 8And this shows how OP's code worked
In [4]: while l: ...: for elem in l: ...: l.remove(elem) ...: print(elem) ...: 0 2 4 6 8 1 5 9 3 7
Test everything in a Python shell (iPython, Azure Notebook, etc.)
- Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
- Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
- You posted a claim that something you did not test works? Be prepared to eat your hat.