RuntimeError: dictionary changed size during iteration
RuntimeError: dictionary changed size during iteration in Python means you added or removed keys from a dict while looping over it. Loop over a copy of the keys, or build a new dict.
How do I fix RuntimeError: dictionary changed size during iteration?
RuntimeError: dictionary changed size during iteration means you added or deleted keys while looping over the same dict. Loop over a snapshot instead with for key in list(d):, or build a new dict with a comprehension, such as {k: v for k, v in d.items() if v != 0}. Check helper functions called inside the loop too.
- 1. Find del or new keysLook inside the loop for del d[key], d.pop(key) or d[new_key] = value.
- 2. Loop over list(d)Change for key in d: to for key in list(d): so the loop walks a fixed copy of the keys.
- 3. Check called functionsA helper called from the loop may delete keys from the same dict. Read what it does.
What does RuntimeError: dictionary changed size during iteration mean?
A dict iterator walks the internal table in order. Adding or removing keys can reorganise that table mid-walk, so Python refuses rather than silently skipping or repeating items. Sets raise the same error. Lists do not raise — they skip items instead, which is worse.
Why does RuntimeError: dictionary changed size during iteration happen?
1. Deleting entries while looping
Removing keys changes the size the iterator is tracking.
Breaks
for key in stock:
if stock[key] == 0:
del stock[key]Works
stock = {k: v for k, v in stock.items() if v != 0}2. Adding derived keys while looping
Adding a key during iteration is just as unsafe as removing one.
Breaks
for name in prices:
prices[name + "_tax"] = prices[name] * 1.2Works
for name in list(prices):
prices[name + "_tax"] = prices[name] * 1.23. Mutating inside a function called from the loop
The loop looks innocent; the helper it calls removes the key.
Breaks
for user_id in sessions:
expire_if_old(user_id) # does del sessions[user_id]Works
for user_id in list(sessions):
expire_if_old(user_id)How to find it in your own code
Iterate over list(d) or list(d.items()) — a snapshot — when you need to modify the dict in the loop. When you are filtering, a dict comprehension that builds a new dict is cleaner and has no ordering surprises.
Try it: fix this bug
easypurge_inactive_sessions({"s1": True, "s2": False}) raises RuntimeError: dictionary changed size during iteration.
def purge_inactive_sessions(sessions):
for token, active in sessions.items():
if not active:
del sessions[token]
return sessionsMore challenges with this error
Each of these challenges runs into this error. Find the bug and fix it, no install and no account needed.
Still not sure why yours breaks?
Paste it into the visualizer and watch it run line by line, with every variable at every step. Free, and it runs in your browser.