How do you add a Dictionary of items into another Dictionary
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
In Python, "adding one dictionary into another" usually means merging key-value pairs. The correct method depends on intent: sometimes you want to mutate the original dictionary, sometimes you want a new merged result, and sometimes overlapping keys should be combined instead of overwritten.
Use update for In-Place Merging
If you want to modify the destination dictionary directly, dict.update is the standard tool.
The result is:
Notice the important rule: if the same key appears in both dictionaries, the incoming value replaces the old one.
That makes update good for overrides, but not for additive logic such as counting or stock accumulation.
Build a New Dictionary Instead of Mutating
If you want to keep both inputs unchanged, create a new merged dictionary.
In Python 3.9 and later, the merge operator is the cleanest form.
For older versions, unpacking works the same way.
In both cases, values from the right-hand side win for duplicate keys.
Combine Values Instead of Overwriting Them
If duplicate keys should be added together or otherwise merged by custom logic, write that policy explicitly.
Now the duplicate key bananas becomes 7 instead of being overwritten with 2.
This is clearer than forcing update to do something it was not designed to do.
Nested Dictionaries Are a Different Problem
Standard dictionary merges are shallow. If a value is itself a dictionary, the whole nested mapping is replaced unless you implement recursion deliberately.
The db dictionary from left is replaced completely. If that is not what you want, use a recursive merge.
This preserves host while updating only port.
Choose the Merge Policy Intentionally
A lot of confusion comes from assuming there is one universal "dictionary merge" operation. There is not.
A practical rule set is:
- use
updatewhen mutating the destination is fine - use
|or unpacking when you want a new dictionary - use a custom loop when duplicate keys need special handling
- use recursion when nested dictionaries should be merged rather than replaced
The important design question is not syntax. It is what should happen when keys overlap.
Common Pitfalls
Using update and forgetting that it mutates the original dictionary is a common mistake.
Assuming duplicate keys are combined rather than overwritten is another.
Using a shallow merge on nested configuration data also causes subtle bugs because inner dictionaries get replaced entirely.
Finally, when working across Python versions, remember that the | merge operator requires Python 3.9 or later.
Summary
- '
updatemerges one dictionary into another by mutating the target' - '
|and unpacking create a new merged dictionary instead' - duplicate keys are overwritten unless you define custom merge logic
- nested dictionaries need explicit deep-merge behavior if replacement is not acceptable
- choose the method based on the meaning of duplicate keys, not only on syntax convenience
Related reading
- How do you address messages coming out of order in a message queue?
- How do you cast a List of supertypes to a List of subtypes?
- How do you convert a byte array to a hexadecimal string, and vice versa?
- How do you convert a string to a byte array in .NET?
- How do you alter the size of a Pytorch Dataset?
- How do you convert a time.struct_time object into a datetime object?
- How do you create a dictionary in Java?
- How do you create nested dict in Python?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.