Merge Dictionaries in Python with | and update()
Combine default settings with user choices in one expression, and see which value wins when keys clash.
- Merging with the | operator (Python 3.9+)
- Updating in place with |= and update()
- Which value wins for duplicate keys
- theme: dark
- language: en
- language: vi
- font_size: 16
- theme: dark
- language: vi (from preferences)
- font_size: 16
The right side wins
The problem
Settings often come from several places: defaults built into the program, then the user's own choices on top. You need one combined result where the user's choices win, without changing the defaults that other users still need.
Think of it like this
Merging dictionaries is like laying a sheet of stickers over a printed form. Where the sticker covers a field, you see the sticker's value. Where there is no sticker, the original print shows through. The form underneath is not damaged.
defaults📄 The printed formpreferences🏷️ The stickers on topdefaults | preferences👀 What you see: stickers win where they overlap"language": "vi"🔁 The sticker hides the printed enThe | operator merges two dictionaries into a new one; when a key is in both, the value from the right-hand dictionary is kept.
Key points
defaults | preferencescreates a new dictionary; neitherdefaultsnorpreferenceschanges."language"is in both, so the value from the right side,"vi", wins."theme"comes only fromdefaultsand"font_size"only frompreferences, so both are kept.- To change
defaultsitself, usedefaults.update(preferences)ordefaults |= preferences.
When should you use it?
- Applying user settings on top of default settings.
- Combining configuration from a file, environment variables and command-line options.
- Updating a record with only the fields that changed.
When not to use it
- Nested settings (a dictionary inside a dictionary):
|replaces the inner dictionary as a whole instead of merging it. - Python older than 3.9, which has no
|for dictionaries: use{**defaults, **preferences}.
See how it works
Type a key and store or look it up. Python hashes the key to find its slot directly, without searching through every entry.
Example code
defaults = {"theme": "dark", "language": "en"}
preferences = {"language": "vi", "font_size": 16}
settings = defaults | preferences
print(settings)
Result of running the code
{'theme': 'dark', 'language': 'vi', 'font_size': 16}
Code explanation
The union operator creates a new dictionary. When keys overlap, the value from the dictionary on the right wins.
Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.
Practice exercises
Modify the runnable example with the exercises below to build understanding beyond copying the result.
- Change the input and predict the output before running.
- Handle empty or invalid input.
- Wrap the logic in a function and add more test cases.
Common mistakes
Putting the dictionaries in the wrong order
The right side wins. Defaults must come first.
settings = preferences | defaults # defaults overwrite the user
settings = defaults | preferences
Changing the defaults by accident
update() changes the dictionary in place, so the next user starts with the wrong defaults.
defaults.update(preferences)
settings = defaults
settings = defaults | preferences
Finished this lesson?
Mark it as learned to track your progress.
Practice with a challenge
Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.
All challenges