In this Python tutorial, we will understand the use of the Python Dictionary update() method.
The Python Dictionary update() method is used to update the value of an existing key-value pair. However, if the key with the same name does not exist, it will add the key value automatically to the dictionary.
Here we will understand the following topics related to the Python Dictionary update().
- Introduction to Python Dictionary update()
- Syntax of Python Dictionary update()
- Examples of Python Dictionary update()
Introduction to Python Dictionary update()
In this section, we will discuss what is Python Dictionary update().
Python Dictionary is an unordered collection of data values, that is used to store data values like a tuple, which does not like other Data Types that contain only a single value, instead it stores data in key-value pairs.
The dictionary.update() is a Python Dictionary method utilized to update the dictionary with the key and value pairs.
However, it inserts a key-value pair in the dictionary if it is not present. Also, it updates the key-value pair if it already exists in the dictionary.
Next, let us look at the syntax of using the dictionary.update() in Python
Syntax of Python Dictionary update()
dict.update([other])
- It consists of only one parameter.
- other: This parameter can either be a dictionary or an iterable of key-value pairs.
Return value
This function does not return any values, rather it updates the same input dictionary with the newly associated values of the keys.
However, if try to fetch the value returned by dictionary.update(), we will get None as a result.
Read: Python Dictionary sort
Examples of Python Dictionary update()
Now that we understood the syntax of using the dictionary.update(), let us discuss a few examples of using this method in Python.
Example 1: Updating existing key-value in Dictionary
# Defining a Dictionary
user = {
'Name': 'John',
'Age': 27,
'Sex': 'Male',
'Occupation': 'Engineer',
'Country': 'United States'
}
print('Original Dictionary:')
print(user)
# Updating the existing key-value
user.update({'Country': 'United Kingdom'})
# Printing the updated result
print('Updated Dictionary:')
print(user)
In this example, we updated the value of the Country key from the United States to the United Kingdom using the dictionary.update() method.
Once we run the above Python program, we will get the following result.
Original Dictionary: {'Name': 'John', 'Age': 27, 'Sex': 'Male', 'Occupation': 'Engineer', 'Country': 'United States'} Updated Dictionary: {'Name': 'John', 'Age': 27, 'Sex': 'Male', 'Occupation': 'Engineer', 'Country': 'United Kingdom'}
Also, check: Python Dictionary index
Example 2: Updating a dictionary using another dictionary
# Defining Dictionary-1
user = {
'Name': 'John',
'Age': 27
}
# Defining Dictionary-2
user_details = {
'Sex': 'Male',
'Occupation': 'Engineer',
'Country': 'United States'
}
# Updating a dictionary-1 using dictionary-2
user.update(user_details)
# Printing the final result
print(user)
In the above example, we defined 2 dictionaries- user and user_details. After this, we used the update() method on the user dictionary to add the data of user_details to the user.
In the end, we will get the user dictionary as follows in Python.
{'Name': 'John', 'Age': 27, 'Sex': 'Male', 'Occupation': 'Engineer', 'Country': 'United States'}
Read: Python dictionary multiple values
Example 3: Updating a dictionary using key-value pairs
# Defining a Dictionary
user = {
'Name': 'James',
'Age': 26
}
# Updating a dictionary using key-value pairs
user.update(
Sex='Male',
Country= 'United States'
)
# Printing the final result
print(user)
In this example, instead of adding key-value pairs as a dictionary, we specify key-value pairs directly in the update() method.
Once we execute the above Python program, we will get the following result.
{'Name': 'James', 'Age': 26, 'Sex': 'Male', 'Country': 'United States'}
Example 4: Updating a dictionary using a list of tuples
# Defining Dictionary-1
user = {
'Name': 'Rogers',
'Age': 32
}
# Defining Dictionary-2
info = [('Sex', 'Male'), ('Country', 'United States')]
# Updating a dictionary using a list of tuples
user.update(info)
# Printing the final result
print(user)
In this example, we defined a list of tuples where each tuple represents a key-value pair. After this, we utilized the list of tuples in the update() method to add these key-value pairs to the user dictionary.
The final result of the above python program is given below.
So, in this section, we understood how to use the dictionary.update() method in Python to update the key-value pairs of an existing Python Dictionary.
You may like the following Python tutorials:
Conclusion
So, in this Python tutorial, we understood what Python Dictionary update() method is. Here we covered the syntax of using the dictionary.update() and various examples of how to update dictionary values using it.
Here is the set of topics that we covered.
- Introduction to Python Dictionary update()
- Syntax of Python Dictionary update()
- Examples of Python Dictionary update()
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.