clear() method for dictionary

All dictionary methods

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


plus2net.com



Post your comments , suggestion , error , requirements etc here





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 FORUM . Contact us
©2000-2021 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer