Python Dictionary clear() Method

my_dict.clear() removes every key-value pair from the dictionary in place and returns None. The dictionary object itself still exists after it is cleared.

See the complete Dictionary Methods table for the other dictionary operations in this cluster.

my_dict={'a':'Alex','b':'Ronald','c':'Ron'}
my_dict.clear()
print(my_dict)
Output is here
{}
We can check the data type of the output
my_dict={'a':'Alex','b':'Ronald','c':'Ron'}
my_dict.clear()
print(type(my_dict))
Output
<class 'dict'>

Difference between del and clear

clear is a method of dictionary class, del is a keyword in Python. After using clear the list is not deleted but after using del the dictionary object is deleted.
my_dict={'a':'Alex','b':'Ronald','c':'Ron'}
del my_dict
print(my_dict) #NameError
The last line will generate error as we can't print the dictionary after deleting the same.

All dictionary methods


Subscribe to our YouTube Channel here



plus2net.com







Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer