Python dictionary update() method is used to update the dictionary using another dictionary or an iterable of key-value pairs(such as a tuple). If the key is already present in the dictionary, the update() method changes the existing key with the new value.
Python dictionary is an unordered collection of elements, that is used to store elements like key-value pairs, which is not the same as other data types that contain only a single value.
In this article, I will explain the Python dictionary update()
method with examples. It does not return any values, in addition, it updates the same dictionary with the newly associated key-value pairs
All dictionary methods are available on the dictionary methods page..
Quick Examples of Python Dictionary update()
Following are the quick examples of the Python dictionary update() method.
# Example 1: Update the dictionary
Technology = {'course': 'python', 'fee':4000}
Technology.update({'duration':'45days'})
# Example 2: Update the existing key of the dictionary
Technology= {'course': 'python', 'fee': 4000, 'duration': '45days'}
Technology.update({'duration':'60 days'})
# Example 3: Update using key-value pair
Technology= {'course': 'python', 'fee': 4000,}
Technology.update([('duration','45days'),('tutor','Richard')])
1. Syntax of Dictionary update()
Following is the syntax of the Python dictionary update() method.
# syntax of the dictionary update()
dict.update(iterable)
1.2 Parameters of Python Dictionary update()
Python dictionary update() allows one parameter.
Iterable
:(optional) It is either dictionary or an iterable key-value pair.
1.3 Return Value of Python Dictionary update()
It returns None
.
2. Usage of Python Dictionary update() Method
Using Python Dictionary update()
method we can update the dictionary by using another dictionary or an iterable of key-value pairs(such as a tuple). If the key is already present, this method updates the existing key with the new value from iterable value.
2.1 Add New Element to the Dictionary
The below example demonstrates how to add new elements to the existing dictionary.
# Using update() update the dictionary
Technology = {'course': 'python', 'fee':4000}
Technology.update({'duration':'45days'})
print("Updated Technology:",Technology)
# Outputs:
Updated Technology: {'course': 'python', 'fee': 4000, 'duration': '45days'}
2.2 Update the Existing key of Dictionary
When key
is already present in the dictionary update()
method updates the existing key with key-value pair. Let’s see with an example.
# update the existing key of the dictionary
Technology= {'course': 'python', 'fee': 4000, 'duration': '45days'}
Technology.update({'duration':'60 days'})
print("Updated Technology:",Technology)
# Outputs:
Updated Technology: {'course': 'python', 'fee': 4000, 'duration': '60 days'}
In this example duration
key existed with the value 45 days
, which has been updated with the value 65 days
.
3. Using tuple as a Param
Update() method also updates the dictionary when passing an iterable object of key-value
pair(tuple) as a parameter. Let’s take an example.
# update the dictionary by passing iterable of key-value pair
Technology= {'course': 'python', 'fee': 4000,}
Technology.update([('duration','45days'),('tutor','Richard')])
print("Updated Technology:",Technology)
# Output:
Updated Technology: {'course': 'python', 'fee': 4000, 'duration': '45days', 'tutor': 'Richard'}
From the above code, we have passed tuple
as a param to Python dictionary update() method, this added new elements duration
and tutor
to the dictionary.
4. Conclusion
In this article, I have explained the Python Dictionary update()
method with examples. By using a dictionary or an iterable of key-value pairs(such as a tuple) to update the dictionary? I have also explained how to update the existing key
with updated key-value
pair.
Happy Learning !!
Related Articles
- get() Method of Python Dictionary
- clear() Method of Python Dictionary
- items() Method of Python Dictionary
- fromkeys() Method of Python Dictionary
- keys() Method of Python Dictionary
- values() Method of Python Dictionary
- copy() Method of Python Dictionary
- pop() Method of Python Dictionary
- setdefault() Method of Python Dictionary
- popitem() Method of Python Dictionary
- Python Set copy() Method
This is a great post! I was wondering if you could tell me how to update a dictionary in Python.