KnowledgeBoat Logo

Computer Science

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

Python Dictionaries

7 Likes

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.

Answered By

4 Likes


Related Questions