Why does Python skip elements when I modify a list while iterating over it?

This is a well-documented behaviour in Python, that you aren't supposed to modify the list being iterated through. Try this instead:

for i in x[:]:
    x.remove(i)

The [:] returns a "slice" of x, which happens to contain all its elements, and is thus effectively a copy of x.


When you delete an element, and the for-loop incs to the next index, you then skip an element.

Do it backwards. Or please state your real problem.


I think, broadly speaking, that when you write:

for x in lst:
    # loop body goes here

under the hood, python is doing something like this:

i = 0
while i < len(lst):
    x = lst[i]
    # loop body goes here
    i += 1

If you insert lst.remove(x) for the loop body, perhaps then you'll be able to see why you get the result you do?

Essentially, python uses a moving pointer to traverse the list. The pointer starts by pointing at the first element. Then you remove the first element, thus making the second element the new first element. Then the pointer move to the new second – previously third – element. And so on. (it might be clearer if you use [1,2,3,4,5] instead of [1,2,2,2,2] as your sample list)


Why don't you just use:

x = []

It's probably because you're changing the same array that you're iterating over.

Try Chris-Jester Young's answer if you want to clear the array your way.


I know this is an old post with an accepted answer but for those that may still come along...

A few previous answers have indicated it's a bad idea to change an iterable during iteration. But as a way to highlight what is happening...

>>> x=[1,2,3,4,5]
>>> for i in x:
...     print i, x.index(i)
...     x.remove(i)
...     print x
...
1 0
[2, 3, 4, 5]
3 1
[2, 4, 5]
5 2
[2, 4]

Hopefully the visual helps clarify.