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
Can you check for a value inside a dictionary using in operator? How will you check for a value inside a dictionary using in operator ?
Dictionary is a mutable type, which means you can modify its contents ? What all is modifiable in a dictionary ? Can you modify the keys of a dictionary ?
How is
clear( )
function different fromdel <dict>
statement ?What does fromkeys( ) method do?