Updating Dictionary in Python

PythonServer Side ProgrammingProgramming

You can update a dictionary by adding a new entry or a key-value pair, modifying an existing entry, or deleting an existing entry as shown below in the simple example −

Example

 Live Demo

#!/usr/bin/python
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
dict['Age'] = 8; # update existing entry
dict['School'] = "DPS School"; # Add new entry
print "dict['Age']: ", dict['Age']
print "dict['School']: ", dict['School']

Output

When the above code is executed, it produces the following result −

dict['Age']: 8
dict['School']: DPS School
raja
Published on 28-Jan-2020 12:50:23
Advertisements