CBSE Class 12 Computer Science Question 70 of 105

Python Revision Tour II — Question 19

Back to all questions
19
Question

Question 19

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

Answer

del D deletes the entire dictionary D. After executing del D, the variable D is no longer defined, and any attempt to access D will result in a NameError.
del D[<key>] deletes the key-value pair associated with the specified key from the dictionary D. After executing del D[<key>], the dictionary D still exists, but the specified key and its corresponding value are removed from the dictionary.

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.