dict.copy() / dict() / Dict Merge
This page covers how to copy, create, and merge dictionaries. Python 3.9 and later also support a simpler merge using the | operator.
Syntax
# Shallow-copies a dictionary.
new_dict = dictionary.copy()
# Creates a new dictionary from keyword arguments or an existing dictionary.
new_dict = dict(key=value, ...)
new_dict = dict(dictionary)
# Merges multiple dictionaries using the unpacking operator.
merged = {**dict1, **dict2}
# Merges two dictionaries using the | operator (Python 3.9+).
merged = dict1 | dict2
Functions and Operators
| Function / Operator | Description |
|---|---|
| dict.copy() | Creates and returns a shallow copy of the dictionary. Changes to the original do not affect the copy, but nested mutable values are still shared. |
| dict(key=value) | Creates a dictionary from keyword arguments. Keys must be strings. |
| dict(dictionary) | Creates a new dictionary from an existing dictionary or iterable. |
| {**dict1, **dict2} | Creates a new dictionary by merging multiple dictionaries using the unpacking operator. If a key appears in both, the value from the later dictionary takes precedence. |
| dict1 | dict2 | Returns a new dictionary that merges two dictionaries (Python 3.9+). If a key appears in both, the value from the right-hand dictionary takes precedence. |
| dict1 |= dict2 | Merges the contents of dict2 into dict1 in place (Python 3.9+). |
Sample Code
# Create a shallow copy with copy().
original = {'name': 'Alice', 'age': 30}
copied = original.copy()
copied['name'] = 'Bob'
print(original['name']) # Prints 'Alice' — the original is unchanged.
print(copied['name']) # Prints 'Bob'.
# Create a dictionary with dict().
d1 = dict(name='Carol', age=25)
print(d1) # Prints {'name': 'Carol', 'age': 25}.
# Merge dictionaries using the unpacking operator.
base = {'color': 'blue', 'size': 'M'}
extra = {'size': 'L', 'weight': 60}
merged = {**base, **extra}
print(merged) # Prints {'color': 'blue', 'size': 'L', 'weight': 60}.
# Merge with the | operator (Python 3.9+).
d2 = {'x': 1, 'y': 2}
d3 = {'y': 99, 'z': 3}
result = d2 | d3
print(result) # Prints {'x': 1, 'y': 99, 'z': 3}.
# Merge in place with |=.
settings = {'theme': 'dark', 'lang': 'ja'}
overrides = {'lang': 'en', 'font': 'mono'}
settings |= overrides
print(settings) # Prints {'theme': 'dark', 'lang': 'en', 'font': 'mono'}.
Notes
dict.copy() creates a shallow copy. If the values are immutable objects such as strings or numbers, modifying one copy does not affect the other. However, if a value is a mutable object such as a list or another dictionary, the reference is shared between the two copies. Use copy.deepcopy() when a deep copy is required.
There are several ways to merge dictionaries. The {**d1, **d2} syntax works from Python 3.5 onward and can merge three or more dictionaries at once. Python 3.9 and later allow the more readable d1 | d2 syntax. In either case, if a key exists in both dictionaries, the value from the later dictionary takes precedence. Be careful not to overwrite values unintentionally.
For creating dictionaries with comprehensions, see Dictionary Comprehensions.
If you find any errors or copyright issues, please contact us.