Python Programming
About Lesson

Updating Dictionary
Updating of dictionary involves adding a new key-value pair or modifying an existing item or deleting an item.

#Example code
Dict1 = {‘Name’: ‘Ram’, ‘Age’: 22, ‘Class’: ‘First’}
Dict1[‘Age’] = 28; # update existing entry
Dict1[‘College’] = “Govt Degree College”; # Add new entry
print (“Dict1[‘Age’]: “, Dict1[‘Age’])
print (“Dict1[‘College’]: “, Dict1[‘College’])

Delete Dictionary Elements
Removal of elements of dictionary can be done by the following ways:
1. Using del keyword
2. Using clear() method

#Example code
dct = {‘Name’: ‘Krishna’, ‘Age’: 7, ‘Class’: ‘First’}
del dct[‘Name’]; # remove entry with key ‘Name’
print(dct)
dct.clear(); # remove all entries in dict
print (“dct[‘Age’]: “, dct[‘Age’])
del dct ; # delete entire dictionary
print (“dct[‘Class’]: “, dct[‘Class’])

You cannot copy content of this page