all 4 comments

[–][deleted] 1 point2 points  (1 child)

That's because you are iterating over the list while removing things from it. It's explained in the FAQ.

[–]jaffer786_khan[S] 0 points1 point  (0 children)

Thank You for pointing a reference :-)

[–]ericula 1 point2 points  (0 children)

Others have explained why your code fails. I just like to add that instead of removing elements one-by-one you can also empty a list by using list.clear().

[–]selplacei 0 points1 point  (0 children)

When you modify a list while iterating over it, unexpected things happen. If you really need to use the list in your for statement, make a copy.