Last modified: Jan 27, 2026 By Alexander Williams
Python Dict Update Method Guide & Examples
The Python dictionary is a fundamental data structure. It stores data as key-value pairs. One of its most powerful methods is update(). This method lets you modify and merge dictionaries seamlessly.
This guide explains the update() method in detail. You will learn its syntax, see practical examples, and understand common use cases. This knowledge is key for efficient Python programming.
What is the dict update() Method?
The update() method modifies a dictionary in place. It adds new key-value pairs or updates the values of existing keys. The source can be another dictionary, an iterable of pairs, or keyword arguments.
It does not return a new dictionary. Instead, it alters the original one. This is an important distinction from other operations that create copies.
Basic Syntax of update()
The method's syntax is straightforward. You call it on a dictionary object.
# Basic Syntax
dict.update([other])
The other parameter can be another dictionary, an iterable, or keyword arguments. The method is flexible in what it accepts.
Updating with Another Dictionary
The most common use is merging two dictionaries. The update() method adds all items from the source dictionary into the target.
# Example 1: Update with another dictionary
user_profile = {"name": "Alice", "age": 30}
new_data = {"age": 31, "city": "New York"} # 'age' will be updated, 'city' added
user_profile.update(new_data)
print(user_profile)
{'name': 'Alice', 'age': 31, 'city': 'New York'}
Notice how the value for the key "age" changed from 30 to 31. The key "city" was added. The original dictionary was modified directly.
Updating with an Iterable of Key-Value Pairs
You can also use an iterable, like a list of tuples. Each tuple should contain two elements: a key and a value.
# Example 2: Update with a list of tuples
config = {"debug": False}
new_settings = [("debug", True), ("version", 1.1)]
config.update(new_settings)
print(config)
{'debug': True, 'version': 1.1}
This is useful when your data comes in pairs from another source. For more on structuring data, see our guide on how to Import Lists into Python Dictionary.
Updating with Keyword Arguments
You can pass key-value pairs directly as keyword arguments to update(). The keys must be valid Python string identifiers.
# Example 3: Update with keyword arguments
product = {"id": 101}
product.update(name="Laptop", price=999.99, in_stock=True)
print(product)
{'id': 101, 'name': 'Laptop', 'price': 999.99, 'in_stock': True}
This provides a clean, readable way to add a few specific items. It's great for small, ad-hoc updates.
Handling Key Conflicts During Update
A core behavior of update() is how it handles existing keys. If a key from the source already exists in the target, its value is overwritten. New keys are simply added.
# Example 4: Demonstrating overwrite behavior
inventory = {"apples": 10, "bananas": 5}
delivery = {"oranges": 20, "apples": 15} # 'apples' value will change
inventory.update(delivery)
print("Updated Inventory:", inventory)
Updated Inventory: {'apples': 15, 'bananas': 5, 'oranges': 20}
The value for "apples" was updated from 10 to 15. The key "oranges" was added. The key "bananas" remained unchanged.
Merging Multiple Dictionaries
You can chain updates to merge several dictionaries into one. This is a common pattern for consolidating data from different sources.
# Example 5: Merging three dictionaries
defaults = {"theme": "light", "language": "en"}
user_prefs = {"language": "fr"}
session_settings = {"font_size": 12}
final_config = defaults.copy() # Start with a copy to preserve original
final_config.update(user_prefs)
final_config.update(session_settings)
print("Final Configuration:", final_config)
Final Configuration: {'theme': 'light', 'language': 'fr', 'font_size': 12}
Note we used copy() first. This prevents modifying the original defaults dictionary. It's a good practice when you need the original data elsewhere.
Important Behaviors and Common Pitfalls
Understanding these points will help you avoid bugs.
1. In-Place Modification: The update() method changes the dictionary it is called on. It returns None. Do not assign its result to a variable expecting a dictionary.
# Incorrect usage
dict_a = {"a": 1}
dict_b = {"b": 2}
result = dict_a.update(dict_b) # result will be None!
print(result)
None
2. Dictionary Order: In modern Python (3.7+), dictionaries maintain insertion order. The update() method preserves this. Updated keys keep their original position. New keys are added at the end. Learn more about this in our article Is Python Dict in Order of Append?.
3. No "Remove" Action: The update() method only adds or changes keys. It cannot remove keys. If you need to remove items, use methods like pop() or del. For related issues, see Python Dict Has No Attribute Remove: Fix.
Comparison with Other Dictionary Methods
The update() method is part of a rich set of tools for dictionaries. It's important to know when to use it versus other methods.
Use update() for bulk modifications from another mapping. Use simple assignment (dict['key'] = value) for changing a single known key. Use the setdefault() method to safely insert a key only if it doesn't exist.
For a complete overview, explore our Python Dictionary Methods Guide & Examples.
Practical Example: Building a Configuration System
Let's see a realistic example. We'll build a configuration system that layers settings.
# Practical Example: Layered configuration
system_defaults = {
"host": "localhost",
"port": 8080,
"logging": {"level": "INFO", "file": "app.log"}
}
user_file_config = {
"port": 9000,
"logging": {"level": "DEBUG"} # Nested dictionary update
}
environment_config = {"host": "prod-server.com"}
# Start with defaults, then apply user file, then environment vars
final_config = system_defaults.copy()
final_config.update(user_file_config)
final_config.update(environment_config)
print("Final App Config:")
for key, value in final_config.items():
print(f" {key}: {value}")
Final App Config:
host: prod-server.com
port: 9000
logging: {'level': 'DEBUG', 'file': 'app.log'}
Important Note: When the value is itself a dictionary (like "logging"), the entire nested dictionary is replaced. The update() method does a shallow merge. For deep merging of nested dictionaries, you need a custom recursive function.
Conclusion
The Python dictionary update() method is a versatile and essential tool. It provides a clean way to modify dictionaries by merging in data from other sources.
Remember its key behaviors: it updates in place, overwrites existing keys, and accepts multiple input types. Use it for combining configurations, refreshing data, and applying batches of changes.
Mastering update() alongside other dictionary methods will make you a more effective Python programmer. It helps you write cleaner, more efficient code for data manipulation tasks.