What happens if you modify a collection while looping over it?
A dict raises RuntimeError: dictionary changed size during iteration. A list does something worse: it silently skips elements, because removing one shifts everything left while the index keeps advancing. Iterate over a copy, or build a new collection.
Overview
Two different failures
A dictionary keeps a version counter and compares it on each step, so a size change during iteration is caught immediately and loudly. That is a feature: the alternative is undefined behaviour, because a resize can move every entry to a different slot.
A list has no such check. Deleting element i shifts everything after it one place left, while the loop's internal index advances — so the element that moved into position i is never visited. You get a wrong answer and no error at all, which is the harder bug to find.
Step through it
What to watch
- The dict records its size and checks it on every step.
- The error names the real cause — a size change.
- Rebuilding rather than mutating avoids the question entirely.
Say this out loud
"Dicts raise RuntimeError. Lists don't - they silently skip elements, because deleting shifts the rest left while the index moves right. I iterate over list(d) or a slice copy, or better, build a new collection with a comprehension."