Python Dictionary update() method
Python Dictionary update() method
The dictionary.update() method in Python inserts the specified items to the dictionary.
Syntax
dictionary.update(iterable)
Parameter
iterable- This parameter represents a dictionary or an iterable object with key value pairs, that will be inserted to the dictionary.
Return
None
Example 1
# Python program explaining # the dictionary.update() method # initializing the dictionary student = {'studentName': 'Reema','Roll No':'15cs1029'} print("The actual Student Details:",student) # initializing the second dicitonary with updated values updatedStudent= {'studentName': 'Reema','Roll No':'15ME1028','age':22} # update the value of key 'Roll' student.update(updatedStudent) print("Student details after updation:") print(student)
Output
The actual Student Details: {'studentName': 'Reema', 'Roll No': '15cs1029'} Dictionary after updation: {'studentName': 'Reema', 'Roll No': '15ME1028', 'age': 22}
Example 2
# Python program explaining # the dictionary.update() method # Initializing the dictionary with single item Value = { 'A' : 'Reema'} # The Dictionary before Updation print("Original Dictionary:") print(Value) # update the Dictionary with iterable Value.update(B = 'Writing', C = '22') print("Dictionary after updation:") print(Value)
Output
Original Dictionary: {'A': 'Reema'} Dictionary after updation: {'B': 'Writing', 'C': '22', 'A': 'Reema'}