CBSE Class 11 Computer Science Question 70 of 112

Dictionaries — Question 10

Back to all questions
10
Question

Question 10

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

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.