Python Dictionary update() method updates the dictionary with the elements from another dictionary object or from an iterable of key/value pairs.
Example:
Original dictionary : {'A': 'Geeks', 'B': 'For'}
Updated dictionary : {'A': 'Geeks', 'B': 'Geeks'}
Original dictionary : {'A': 'Geeks', 'B': 'For'}
Updated dictionary : {'A': 'Geeks', 'B': 'For', 'C': 'Geeks'}
Syntax of Python Dictionary Update Method
The dictionary update() method in Python has the following syntax:
Syntax: dict.update([other])
Parameters: This method takes either a dictionary or an iterable object of key/value pairs (generally tuples) as parameters.
Returns: It doesn’t return any value but updates the Dictionary with elements from a dictionary object or an iterable object of key/value pairs.
Python Dictionary update() Example
Let us see a few examples of the update() method to update the data of the Python dictionary.
Update with another Dictionary
Here we are updating a dictionary in Python using the update() method and passing another dictionary to it as parameters. The second dictionary is used for the updated value.
Python3
Dictionary1 = { 'A' : 'Geeks' , 'B' : 'For' , }
Dictionary2 = { 'B' : 'Geeks' }
print ( "Original Dictionary:" )
print (Dictionary1)
Dictionary1.update(Dictionary2)
print ( "Dictionary after updation:" )
print (Dictionary1)
|
Output:
Original Dictionary:
{'A': 'Geeks', 'B': 'For'}
Dictionary after updation:
{'A': 'Geeks', 'B': 'Geeks'}
Update with an Iterable
In this example, instead of using another dictionary, we passed an iterable value to the update() function.
Python3
Dictionary1 = { 'A' : 'Geeks' }
print ( "Original Dictionary:" )
print (Dictionary1)
Dictionary1.update(B = 'For' , C = 'Geeks' )
print ( "Dictionary after updation:" )
print (Dictionary1)
|
Output
Original Dictionary:
{'A': 'Geeks'}
Dictionary after updation:
{'A': 'Geeks', 'B': 'For', 'C': 'Geeks'}
Python Dictionary Update Value if the Key Exists
In this example, we will update the value of a dictionary in Python if the particular key exists. If the key is not present in the dictionary, we will simply print that the key does not exist.
Python3
def checkKey( dict , key):
if key in dict .keys():
print ( "Key exist, " , end = " " )
dict .update({ 'm' : 600 })
print ( "value updated =" , 600 )
else :
print ( "Not Exist" )
dict = { 'm' : 700 , 'n' : 100 , 't' : 500 }
key = 'm'
checkKey( dict , key)
print ( dict )
|
Output:
Key exist, value updated = 600
{'m': 600, 'n': 100, 't': 500}
Python Dictionary Update Value if the Key doesn’t Exist
Here, we will try to update the value of the dictionary whose key does not exist in the dictionary. In this case, the key and value will be added as the new element in the dictionary.
Python3
def checkKey( dict , key):
if key not in dict .keys():
print ( "Key doesn't exist So, a new Key-Value pair will be created" )
dict .update({key: 600 })
else :
print ( "Key Exist" )
dict = { 'm' : 700 , 'n' : 100 , 't' : 500 }
key = 'k'
checkKey( dict , key)
print ( dict )
|
Output:
Key doesn't exist So, a new Key-Value pair will be created
{'m': 700, 'n': 100, 't': 500, 'k': 600}
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
05 Jul, 2023
Like Article
Save Article