RuntimeError dict Python Origin: Application & API Last updated 13 Oct 2026

RuntimeError: dictionary changed size during iteration

A dict (or set) was modified while being looped over.

Exact message you may see dictionary changed size during iterationSet changed size during iteration
Seen on: Python

Meaning

Adding or deleting keys during iteration invalidates the iterator. Iterate over a copy of the keys, or build a new dict.

Common causes

  • del d[k] inside for k in d
  • Adding keys while iterating
  • Another thread mutating the dict

⚡ Quick fix

  1. Iterate over list(d) or list(d.items())
  2. Build a new dict with a comprehension
  3. Collect keys to delete, then delete after the loop

Detailed fix by platform

Python

  1. active = {k: v for k, v in users.items() if v.active}

How to diagnose

  1. Loop — Which loop mutates the dict?
  2. Threads — Is another thread writing to it?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

AttributeError — AttributeError: 'X' object has no attribute 'y' (Python)AttributeErrorCollection was modified — InvalidOperationException: Collection was modified; enumeration operation may not execute (.NET)Collection was modifi…ConcurrentModificationException — java.util.ConcurrentModificationException (Java)ConcurrentModificatio…ConnectionRefusedError — ConnectionRefusedError: [Errno 111] Connection refused (Python)ConnectionRefusedErrorEvent loop is closed — RuntimeError: Event loop is closed (Python)Event loop is closedIndex was out of range — ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection (.NET)Index was out of rangeTypeError not iterable — TypeError: x is not iterable (JavaScript)TypeError not iterableunhashable type — TypeError: unhashable type: 'list' (Python)unhashable typeRuntimeEr…

Explore the map of this error Full error map →

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.

💬 What worked for others

No tips yet — did something else fix this for you?

Share what worked for you

Tips are checked by an editor before they appear. Links are removed.