How to iterate through Dictionary and change values?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Updating dictionary values while iterating is a normal Python task, and it is safe as long as the set of keys does not change. The rule to remember is simple: changing existing values is fine, but adding or removing keys while iterating over the live dictionary view can raise errors.
So the real question is not "can I modify a dictionary during iteration?" It is "am I only changing values, or am I also changing the dictionary's size?"
Change Existing Values In Place
If the keys stay the same, iterating through items() and assigning back by key is valid:
This works because the dictionary shape is unchanged. You are only replacing existing values.
Build a New Dictionary When That Is Clearer
Sometimes mutating in place is not the best design. If you want to preserve the original mapping or make the transformation easier to reason about, use a dictionary comprehension:
This is often the better choice in data pipelines and functional-style transformations where preserving inputs matters.
Do Not Add or Remove Keys Mid-Iteration
The dangerous case is changing the key set while iterating directly over the dictionary:
The important detail is list(settings.keys()). That creates a stable snapshot of the keys so removal is safe during the loop.
Without the snapshot, Python can raise RuntimeError: dictionary changed size during iteration.
Rule-Based Updates
If each key needs a different transformation, a rule map is often cleaner than a long chain of if statements:
This makes the per-key behavior explicit and easier to extend.
Nested Dictionaries Need a Different Strategy
For nested data, a recursive transformation helper is often more readable than deeply nested loops:
That approach is usually better than trying to mutate nested structures ad hoc in several separate loops.
Common Pitfalls
The biggest mistake is removing or adding keys while iterating over the live dictionary view. That changes the dictionary's size and can trigger a runtime error.
Another common issue is mutating a shared dictionary in place when the caller expected the original mapping to remain unchanged. If the input should be preserved, build a new dictionary instead.
Developers also sometimes hide many unrelated transformation rules inside one giant loop. Once the rules become complex, a rule map or helper function is usually clearer.
Finally, be careful with nested dictionaries. Simple top-level loops do not scale well once values themselves contain lists or mappings.
Summary
- Updating existing dictionary values during iteration is safe if the key set does not change.
- Use direct assignment for simple in-place value transformations.
- Use a new dictionary when preserving the original mapping is clearer.
- Iterate over a snapshot such as
list(d.keys())when you must add or remove keys. - For nested structures, prefer centralized helper logic over scattered mutation loops.

