Why is it not safe to modify sequence being iterated on?

Without getting too technical:

If you're iterating through a mutable sequence in Python and the sequence is changed while it's being iterated through, it is not always entirely clear what will happen. If you insert an element in the sequence while iterating through it, what would now reasonably be considered the "next" element in the sequence? What if you delete the next object?

For this reason, iterating through a mutable sequence while you're changing it leads to unspecified behaviour. Anything might happen, depending on exactly how the list is implemented. :-)


This is a common problem in many languages. If you have a linear data structure, and you are iterating over it, something must keep track of where you are in the structure. It might be a current index, or a pointer, but it's some kind of finger pointing to the "current place".

If you modify the list while the iteration is happening, that cursor will likely be incorrect.

A common problem is that you remove the item under the cursor, everything slides down one, the next iteration of the loop increments the cursor, and you've inadvertently skipped an item.

Some data structure implementations offer the ability to delete items while iterating, but most do not.


When you modify a collection that you are iterating over the iterator can behave unexpectedly, for example missing out items or returning the same item twice.

This code loops indefinitely when I run it:

>>> a = [ 'foo', 'bar', 'baz' ]
>>> for x in a:
...    if x == 'bar': a.insert(0, 'oops')

This is because the iterator uses the index to keep track of where it is in the list. Adding an item at the beginning of the list results in the item 'bar' being returned again instead of the iterator advancing to the next item.