This Python article will provide an in-depth understanding of the Python dictionary values() method, which allows us to add new values to a dictionary.
In addition, we also discuss its usage and some practical examples.
Dictionary update() method in Python
Below are the topics that we are doing to discuss in this article:
- Introduction to Python Dictionary update() method
- Syntax of the update() method
- Purpose and use cases of the update() method
Python Dictionary update() method
The update() method is a built-in function in Python that allows you to update the contents of a dictionary by adding new key-value pairs or modifying existing ones. It takes one or more Python dictionaries or iterable objects with key-value pairs as arguments and integrates them into the target dictionary.
The syntax for using the update() method is as follows:
dictionary.update(iterable, **kwargs)
dictionary
: The target dictionary is to be updated.iterable
: A dictionary or an iterable object containing key-value pairs to be added or updated in the target dictionary.**kwargs
: Optional keyword arguments representing additional key-value pairs.
Return:
The update() method does not return any value; instead, it directly modifies the target Python dictionary.
update() method in Python Dictionary Examples
Here are some examples to help you understand the update() method more effectively:
Example#1 Updating a dictionary with another dictionary
Let’s assume we have two Python dictionaries containing the populations of some US states. We want to merge these Python dictionaries and update the population of a particular state.
populations1 = {'California': 39538223, 'Texas': 29145505}
populations2 = {'Texas': 29145506, 'New York': 20201249}
populations1.update(populations2)
print(populations1)
In this example, we have two Python dictionaries with populations of some US states. We update populations1
with populations2
, which modifies the population of Texas and adds the population of New York to populations1
.
Output:
Example#2 Updating a dictionary with an iterable object (list of tuples)
Suppose we have a Python dictionary containing the capitals of some US states and want to update this dictionary with new data provided in a Python list of tuples.
state_capitals1 = {'California': 'Sacramento', 'Texas': 'Austin'}
new_data = [('Texas', 'Austin'), ('New York', 'Albany')]
state_capitals1.update(new_data)
print(state_capitals1)
In this example, we have a Python dictionary with the capitals of some US states, and new data is provided in a Python list of tuples. We update state_capitals1
with new_data
, which adds the capital of New York to state_capitals1
. The capital of Texas remains the same as it was not updated in the new data.
Output:
Example#3 Updating a dictionary using keyword arguments
Let’s say we have a Python dictionary containing abbreviations of some US states, and we want to update this Python dictionary with new abbreviations.
state_abbreviations = {'California': 'CA', 'Texas': 'TX'}
state_abbreviations.update(Texas='TX', NewYork='NY')
print(state_abbreviations)
In this example, we have a Python dictionary with abbreviations of some US states. We update state_abbreviations
using keyword arguments, which adds the abbreviation for New York to state_abbreviations
. The abbreviation of Texas remains the same as it was not updated in the keyword arguments.
Output:
Conclusion
The Python dictionary update() method is a powerful and convenient tool for modifying Python dictionaries. It allows you to add new key-value pairs or update existing ones from other dictionaries, iterable objects, or keyword arguments.
You may also like to read the following articles:
- Python dictionary get() method [With Examples]
- Python dictionary key error
- Python Dictionary duplicate keys
- Python dictionary increment value
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.