Computer Science
How is clear( )
function different from del <dict>
statement ?
Python Dictionaries
10 Likes
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.
Answered By
6 Likes
Related Questions
How is
del D
anddel D[<key>]
different from one another if D is a dictionary ?What does fromkeys( ) method do?
How is pop( ) different from popitem( ) ?
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 ?