clear(): removes all items from a dictionary

clear(), takes no argument.

Returns : None. ( the parent dictionary elements are removed )
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 directory 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
Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



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-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer