When you want to combine two dictionaries into one in Python, there are two approaches: a “destructive” method that updates the original dictionary, and a “non-destructive” method that creates a new dictionary.
This article explains how to implement each method and how they handle duplicate keys.
1. update() Method: Updating an Existing Dictionary (Destructive)
Using the dict.update() method adds the content of another dictionary to the dictionary calling the method. If keys are duplicated, the values in the dictionary passed as an argument will overwrite the existing ones.
Implementation Example: Reflecting Application Settings
This scenario merges user settings (user_preference) into the default settings (default_config) by overwriting them.
# Default application settings
default_config = {
"theme": "light",
"notifications": True,
"version": "1.0.0"
}
# Settings the user wants to change (contains duplicate keys)
# 'theme' is overwritten to 'dark', 'language' is newly added
user_preference = {
"theme": "dark",
"language": "ja"
}
print("--- Before update ---")
print(f"Default: {default_config}")
# Execute merge by rewriting default_config itself
default_config.update(user_preference)
print("\n--- After update ---")
print(f"Merged : {default_config}")
Execution Result
--- Before update ---
Default: {'theme': 'light', 'notifications': True, 'version': '1.0.0'}
--- After update ---
Merged : {'theme': 'dark', 'notifications': True, 'version': '1.0.0', 'language': 'ja'}
2. dict() Constructor or Operators: Creating a New Dictionary (Non-Destructive)
If you want to create a new merged dictionary while keeping the original dictionaries intact, you can use the dict() constructor with unpacking (**) or, if you are using Python 3.9 or later, the merge operator |.
Implementation Example: Integrating Settings (Non-Destructive)
default_config = {"theme": "light", "notifications": True}
user_preference = {"theme": "dark", "language": "ja"}
# Method A: Using dict() and unpacking (Python 3.5+)
# Unpacks both dictionaries to create a new one.
# The latter (user_preference) takes precedence.
merged_config_v1 = dict(**default_config, **user_preference)
# Method B: Using the merge operator | (Recommended for Python 3.9+)
# Intuitive and readable syntax.
merged_config_v2 = default_config | user_preference
print("--- Creating a new dictionary ---")
print(f"Merged V1: {merged_config_v1}")
print(f"Merged V2: {merged_config_v2}")
# The original dictionary is unchanged
print("\n--- No effect on the original dictionary ---")
print(f"Default: {default_config}")
Execution Result
--- Creating a new dictionary ---
Merged V1: {'theme': 'dark', 'notifications': True, 'language': 'ja'}
Merged V2: {'theme': 'dark', 'notifications': True, 'language': 'ja'}
--- No effect on the original dictionary ---
Default: {'theme': 'light', 'notifications': True}
Summary
d1.update(d2): Use this when you want to modifyd1itself.dict(**d1, **d2)ord1 | d2: Use this when you want to create a new dictionary while preserving the original one.
In either case, note that for duplicate keys, the value from the dictionary added later (the argument) takes precedence.
