Updating Dictionary in Python


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

Updated on: 28-Jan-2020

392 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements