In this Python tutorial, you will learn what clear() method of dictionary dict class does, its syntax, and how to use this method to remove all the items in a dictionary, with example programs.
Python Dictionary clear()
Python Dictionary clear() method removes all the items from the dictionary. All data in the dictionary will be lost and the length of the dictionary becomes zero.
In this tutorial, we will learn how to clear all items of a dictionary using clear() method.
Syntax
The syntax of dict.clear() is
dict.clear()
clear() method returns None.
Example
In this example, we will take a dictionary with some initial key:value pairs, and clear all the items by calling clear() method on the dictionary.
Python Program
myDictionary = {'a': 58, 'b': 22, 'c': 39}
myDictionary.clear()
print(myDictionary)
Program Output
{}
Conclusion
In this Python Tutorial, we learned about the syntax and usage of Python Dictionary method dict.clear().