CBSE Class 11 Informatics Practices Question 49 of 66

Dictionary — Question 21

Back to all questions
21
Question

Question 11

How is clear() function different from del 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.