Delete Dictionary Elements in Python

PythonServer Side ProgrammingProgramming

You can either remove individual dictionary elements or clear the entire contents of a dictionary. You can also delete entire dictionary in a single operation.

To explicitly remove an entire dictionary, just use the del statement.


Following is a simple example −

 Live Demo

dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}
del dict['Name']; # remove entry with key 'Name'
dict.clear(); # remove all entries in dict
del dict ; # delete entire dictionary
print "dict['Age']: ", dict['Age']
print "dict['School']: ", dict['School']


This produces the following result. Note that an exception is raised because after del dict dictionary does not exist any more −

Traceback (most recent call last):
File "", line 8, in <module>
print "dict['Age']: ", dict['Age'];
TypeError: 'type' object is unsubscriptable

Note − del() method is discussed in subsequent section.

Published on 28-Jan-2020 17:29:30