clear() method for 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
Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

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