Append a dictionary to a 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, you do not append one dictionary to another the way you append to a list. Dictionaries are mappings, so the usual operation is merge. The main design choice is whether you want to mutate the existing dictionary or create a new one.
That distinction matters because shared dictionaries can create side effects. A merge that looks harmless in one function may unexpectedly change state seen somewhere else.
Use update() for in-place merging
If you want to modify the original dictionary, use update():
Output:
This is concise and efficient when mutating user is exactly what you want.
Create a new dictionary when you want to preserve inputs
If you want both originals to stay unchanged, build a new dictionary instead.
This style is common in configuration assembly and other code paths where immutable-style data flow is easier to reason about.
In Python 3.9 and later, the merge operator is even clearer:
For in-place merging, use |=:
Understand what happens with duplicate keys
When the same key exists in both dictionaries, the later value wins.
The result uses "dark" for theme.
This overwrite rule applies to update(), unpacking, and the merge operator. If key collisions are significant in your domain, check for them explicitly before merging.
Simple merges are shallow
A standard dictionary merge does not recursively combine nested dictionaries. It replaces the entire nested value.
Result:
If you need recursive behavior, write a dedicated helper:
This preserves the nested "host" key while overriding only the nested "port".
Choose the method based on ownership
A good practical rule is:
- use
update()when mutation is intended - use
{**a, **b}ora | bwhen you want a fresh dictionary - use a custom recursive helper when nested merge semantics matter
That keeps side effects explicit and makes it easier to reason about shared state.
Common Pitfalls
The biggest mistake is thinking dictionaries have list-like append behavior. They do not. Merging is key-based, not position-based.
Another issue is forgetting that duplicate keys are overwritten by the later dictionary. If both inputs define the same key, you do not get both values unless you design a custom merge rule.
Developers also assume nested dictionaries will merge recursively by default, but ordinary merge operations are shallow.
Finally, be careful with update() in code that passes dictionaries around widely. Mutating a shared object can create bugs that are hard to trace back to the merge point.
Summary
- Dictionaries are usually merged, not appended.
- Use
update()when you want to modify the original dictionary. - Use unpacking or the
|operator when you want a new merged dictionary. - Later values overwrite earlier ones for duplicate keys.
- Built-in dictionary merges are shallow, so nested dictionaries need a custom strategy.
Related reading
- Append integer to beginning of list in Python
- Append values to a set in Python
- Appending a vector to a vector
- Appending the same string to a list of strings in Python
- Append column to pandas dataframe
- Appending to an empty DataFrame in Pandas?
- Applications of red-black trees
- Are arrays passed by value or passed by reference in Java?

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.