.update()
In Python, the .update()
method adds key-value pairs from another dictionary or an iterable of key-value pairs to the target dictionary. If a key already exists, its value is updated; otherwise, a new key-value pair is added. This method is useful for tasks like updating user profiles, merging configuration settings, or synchronizing data across sources.
Syntax
dict.update([other])
Parameters:
other
: This can be a dictionary or an iterable of key-value pairs (like a list of tuples). If there is a duplicate key indict
andother
, the key-value pair indict
is replaced with the key-value pair inother
.
Return value:
The .update()
method updates the dictionary in place and returns None
.
Example 1: Basic Usage of .update()
method in Python Dictionary
This example uses the .update()
method to add the entries from one dictionary to another:
# Create two dictionariesperson = {'name': 'Alice', 'age': 25}new_info = {'city': 'New York', 'age': 26}# Add the entries from 'new_info' to 'person'person.update(new_info)# Print the modified 'person' dictionaryprint(person)
Here is the output:
{'name': 'Alice', 'age': 26, 'city': 'New York'}
Example 2: Using .update()
with a List of Tuples
This example uses the .update()
method to add the entries from an iterable of key-value pairs (list of tuples) to a dictionary:
# Create a dictionary and a tuplesettings = {'theme': 'light', 'notifications': True}new_settings = [('theme', 'dark'), ('volume', 'medium')]# Add the entries from 'new_settings' to 'settings'settings.update(new_settings)# Print the modified 'settings' dictionaryprint(settings)
Here is the output:
{'theme': 'dark', 'notifications': True, 'volume': 'medium'}
Codebyte Example: Using .update()
with Keyword Arguments
This codebyte example uses the .update()
method with keyword arguments to add entries to a dictionary:
Frequently Asked Questions
1. What if the argument to .update()
is None
or not iterable?
Passing None
or a non-iterable object to .update()
will raise a TypeError
. You must provide a dictionary or an iterable of key-value pairs.
2. Is .update()
thread-safe?
No, .update()
is not thread-safe. If you’re working with shared dictionaries in a multi-threaded environment, use locks to avoid race conditions.
3. Can I chain .update()
calls?
Since .update()
returns None
, it cannot be chained directly. If you want to apply multiple updates, call .update()
separately for each.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours