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.
1. Quick Examples of Python Dictionary update()
If you are in a hurry, below are some quick examples of the Python dictionary update() method.
# Quick examples of python dictionary update()
# 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')])
2. Syntax of Dictionary update()
Following is the syntax of the Python dictionary update() method.
# syntax of the dictionary update()
dict.update(iterable)
2.1 Parameters of Dictionary update()
Python dictionary update() allows one parameter.
Iterable
:(optional) It is either dictionary or an iterable key-value pair.
2.2 Return Value of Dictionary update()
It returns None
.
3. 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.
3.1 Add New Element to the Dictionary
You are using the update()
method to add a new key-value pair to the Technology
dictionary. This code initializes a dictionary called Technology
with keys 'course'
and 'fee'
. Then, the update()
method is used to add a new key-value pair 'duration': '45days'
to the dictionary. Finally, the updated dictionary is printed
# Using update() update the dictionary
Technology = {'course': 'python', 'fee':4000}
Technology.update({'duration':'45days'})
print("Updated Technology:\n",Technology)
Yields below output.
3.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. Using the update()
method to update the value of an existing key in the Technology
dictionary.
This program initializes a dictionary called Technology
with keys 'course'
, 'fee'
, and 'duration'
. Then, the update()
method is used to update the value associated with the key 'duration'
from '45days'
to '60 days'
. Finally, the updated dictionary is printed.
# Update the existing key of the dictionary
Technology= {'course': 'python', 'fee': 4000, 'duration': '45days'}
Technology.update({'duration':'60 days'})
print("Updated Technology:\n",Technology)
# Output:
# 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
.
4. Using tuple as a Param
You can use the update()
method to add multiple key-value pairs to the Technology
dictionary by passing an iterable (a list of tuples).
This code initializes a dictionary called Technology
with keys 'course'
and 'fee'
. Then, the update()
method is used to add multiple key-value pairs (('duration','45days')
and ('tutor','Richard')
) from an iterable (a list of tuples) to the dictionary. Finally, the updated dictionary is printed.
# Update the dictionary by passing iterable of key-value pair
Technology= {'course': 'python', 'fee': 4000,}
Technology.update([('duration','45days'),('tutor','Richard')])
print("Updated Technology:\n",Technology)
# Outputs:
# 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.
Frequently Asked Questions On Python Dictionary update() Method
The update()
method is used to update a dictionary with elements from another dictionary or an iterable (e.g., a list of tuples). It adds new key-value pairs to the dictionary or updates the values of existing keys.
If the key already exists in the dictionary, the update()
method will update the value of that key with the value from the provided dictionary or iterable.
You can provide an iterable (e.g., a list of tuples) to the update()
method, as long as the iterable contains key-value pairs. Each tuple in the iterable should represent a key-value pair.
The update()
method is used for adding or updating key-value pairs; it does not remove keys. If you want to remove a key from a dictionary, you should use the pop()
method or the del
statement.
The update()
method itself does not provide a way to set default values for missing keys. If you want to set default values, you may need to iterate through the items of the other dictionary manually and set default values for missing keys.
The update()
method modifies the original dictionary in-place. It doesn’t return a new dictionary; instead, it updates the calling dictionary with the elements from the provided dictionary or iterable.
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
- Python Set copy() Method
- 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
- fromkeys() Method of Python Dictionary
This is a great post! I was wondering if you could tell me how to update a dictionary in Python.