02 / 06

Why does modifying a list while iterating over it lead to unexpected behavior?

Difficulty: 6/10
List Mutation, Iterator Invalidation, Debugging

Mutating a list during iteration invalidates the iterator's internal index

A Python list iterator stores an internal index that advances by one on each next() call. When you remove or insert elements during iteration, the list shifts, but the iterator's index does not adjust. So the iterator can skip elements, revisit elements, or run past the end. The result is that the loop silently processes the wrong items or raises IndexError. The safe patterns are to iterate over a copy (for x in list(lst)), build a new list with a comprehension, or collect indices to delete and remove them after the loop. In Python 3, list iterators are not designed to detect mutation, so there is no error; you just get wrong behavior.

  1. 1

    Removing an element shifts subsequent items left, so the iterator's next index skips the item that moved into the removed slot.

  2. 2

    Appending during iteration can cause an infinite loop because the iterator keeps finding new elements.

  3. 3

    Iterate over a copy when you must mutate: for x in lst[:] or for x in list(lst).

  4. 4

    Use a list comprehension to build a new list: lst = [x for x in lst if condition].

  5. 5

    Common mistake: assuming Python will raise an error when you mutate during iteration. It does not.

  6. 6

    Common mistake: using remove() inside a for loop over the same list, which skips elements.

  7. 7

    Version note: this behavior is consistent across Python 3 versions; it is a consequence of the iterator protocol.

javascript

Scenario Questions

0-2 years experience

  1. 1You remove an element from a list inside a for loop and notice one element is skipped. Why?
  2. 2How do you safely iterate over a list while removing items?

2-5 years experience

  1. 1A loop appends to a list while iterating and never ends. How do you fix it?
  2. 2You need to filter a list in place. What is the Pythonic way that avoids mutation during iteration?

5-8 years experience

  1. 1You are processing a queue represented as a list and need to remove processed items as you go. How do you structure it safely?
  2. 2A teammate uses list.remove() in a loop and introduces a subtle bug. How do you review and correct it?

8+ years experience

  1. 1Design a data structure that supports safe iteration and mutation concurrently without surprising the consumer.
  2. 2Explain how iterator invalidation manifests in other Python containers (dict, set) and how to handle it.

Follow-up Questions

  • What happens if you append to a list while iterating over it?
  • How would you safely remove multiple items from a list based on a condition?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.