KnowledgeBoat Logo

Computer Science

How is clear( ) function different from del <dict> statement ?

Python Dictionaries

10 Likes

Answer

The clear( ) function removes all the key:value pairs from the dictionary and makes it empty dictionary while del <dict> statement removes the complete dictionary as an object. After del statement with a dictionary name, that dictionary object no more exists, not even empty dictionary.
For example:

d = {1: 'a' , 2 : 'b'} 
d.clear()
print(d)
del d
print(d)
Output
{}
NameError: name 'd' is not defined.

Answered By

6 Likes


Related Questions