How to concatenate two dictionaries to create a new one?
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
Combining dictionaries in Python is simple for flat key-value pairs, but details matter when keys overlap or values are nested. The wrong merge method can silently overwrite data or mutate objects you expected to keep unchanged. This guide compares reliable merge patterns and when to use each one.
Use the Merge Operator for Readable New Dictionaries
In modern Python, the | operator creates a new dictionary from two inputs. If both dictionaries contain the same key, the right-hand dictionary wins.
Output behavior:
leftandrightremain unchanged.merged["port"]becomes5433because right side overrides left side.
This is usually the cleanest syntax for configuration layering, such as defaults plus environment-specific overrides.
Support Older Python With Unpacking
If you need compatibility with Python 3.5 and later, dictionary unpacking provides similar behavior.
Unpacking creates a new dictionary and follows the same key precedence rule where later values override earlier ones.
For dynamic multi-source merges, combine many dictionaries in order:
The final result reflects the last value for each repeated key.
Use copy Plus update When You Need In-Place Flow
update mutates a dictionary, which can be efficient in imperative pipelines. If you still need a new dictionary, clone first.
This style is explicit in multi-step code where you progressively enrich one working object. It is also convenient when merge inputs are produced conditionally.
Handle Nested Dictionaries With Deep Merge Logic
All basic merge methods are shallow. If both sides contain nested dictionaries under the same key, the entire nested object from the right side replaces the left side value.
When you need recursive behavior, implement a deep merge function:
This approach protects nested data and avoids mutation side effects through deep copies.
Choose Merge Semantics Deliberately
Before choosing syntax, define merge rules clearly:
- should right side always override
- should missing keys be preserved
- should nested dictionaries merge recursively
- should lists append, replace, or deduplicate
Encoding these rules in one utility function prevents inconsistent behavior across modules.
For application configuration, merge order should mirror precedence levels. For example: defaults, environment file, secret store, runtime flags. Keeping this order explicit improves predictability and debugging.
Common Pitfalls
- Assuming merges are deep when Python built-in merge methods are shallow.
- Mutating a source dictionary with
updatewhen immutable behavior was expected. - Relying on implicit key precedence instead of documenting source priority.
- Merging dictionaries that contain mutable nested structures without copying.
- Forgetting version compatibility when using the
|operator on older Python versions.
Summary
- Use
|for clear new-dictionary merges in modern Python. - Use unpacking syntax for broad Python 3 compatibility.
- Use
copyplusupdatefor explicit stepwise mutation workflows. - Implement deep merge when nested dictionary values must combine recursively.
- Define and document precedence rules to avoid silent data loss.
Related reading
- How to configure Kafka to behave like a FiFo queue?
- How to configure spring-data-mongodb to use a replica set via properties
- How to consolidate date ranges in a list in C
- How to construct a set out of list items in python?
- How to condense if/else into one line in Python?
- How to configure Celery to run as systemd service with a Django application served by Gunicorn?
- How to consume just one message from rabbit mq on nodejs
- How to convert a binary tree to binary search tree in-place, i.e., we cannot use any extra space

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.