CBSE Class 11 Computer Science Question 69 of 112

Dictionaries — Question 9

Back to all questions
9
Question

Question 9

How is del D and del D[<key>] different from one another if D is a dictionary ?

Answer

del D is used to delete the whole dictionary where as del D[<key>] is used to delete only the key:value pair with the given key.
For example:

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