The safest way to modify a list while iterating over it in Python is to not modify it in place: build a new list with a list comprehension, or iterate over a copy such as for x in items[:]. Adding or removing items from the list you are looping over shifts the indexes under the iterator, so elements get skipped and the result is wrong, usually without any error.
This guide explains why that happens, then shows the reliable patterns for removing, replacing and adding items. Examples target Python 3.13 and 3.14.
A for loop over a list uses an internal index that moves forward by one on every pass. If you delete the current item, everything after it shifts one position to the left, and the index then points past the next item.
This output looks right by luck. Try consecutive even numbers:
The loop removed 2, shifted 4 into index 0, moved on to index 1 (which is now 6), and never looked at 4. Python does not raise an error for a list, unlike a dict or set, which raise RuntimeError if their size changes during iteration.
For filtering and transforming, a list comprehension is the idiomatic choice. It is short, fast and has no mutation problem.
If other code holds a reference to the original list and must see the change, assign into the slice, which keeps the same list object:
When the loop body needs side effects or complex logic, loop over a copy and change the original.
This is correct but not efficient: remove searches from the start, so the loop is quadratic (O(n squared)) on long lists, and remove deletes the first equal value, which is wrong if the list holds duplicates you want to keep. Prefer a comprehension for large lists.
Removing items while walking from the end to the start is safe, because deletions only shift items you have already visited.
This changes the list in place and removes by position, so duplicates are handled correctly. It is still quadratic in the worst case because each del shifts the tail.
Changing the value at an index does not move anything, so replacing items in place is safe. Use enumerate to get the index.
Assigning to the loop variable (price = price * 1.1) only rebinds the name and leaves the list unchanged, which is a common beginner bug.
Appending to a list you are iterating over can create an endless loop, because the new items are visited too. Collect additions in a second list and merge afterwards.
A deliberate work queue is the exception: while queue: item = queue.pop(0) with appends inside is a valid pattern, but use collections.deque and popleft() so the pops are O(1).
| Task | Best pattern |
|---|---|
| Filter items | List comprehension, or filter() |
| Transform every item | List comprehension |
| Replace values at their index | enumerate and assignment |
| Remove with side effects | Iterate over a copy, or go backwards |
| Add items | Collect in a new list, then extend |
The list iterator only tracks an index, not the list's size, so it cannot tell that anything changed. Dictionaries and sets keep a size check, which is why they raise RuntimeError.
Yes. Assigning items[i] = value keeps every index stable, so the iteration is unaffected.
A list comprehension or slice assignment, both linear time. Repeated remove() or del calls are quadratic.
Adding or removing keys while iterating a dict raises RuntimeError: dictionary changed size during iteration. Iterate over list(d) or build a new dict with a comprehension.
Never add or remove items from a list while a plain for loop is walking it. Build a new list with a comprehension, loop over a copy, or iterate backwards by index. Replacing values by index with enumerate is safe, and appends should go to a separate list that you merge after the loop.