Python dictionary clear() is an inbuilt function that removes all items from the dictionary. It takes no parameter. The clear() method doesn’t return any value. If you are new to Python Dictionary, then check out this article. Dictionary is the collection that is unordered, changeable and indexed. Python Dictionary clear() method
The syntax of the clear() method is the following.
dictionary.clear()
Let us see the example.
## app.py
appDict = {
'shopping': 'flipkart',
'transport': 'ola',
'banking': 'paytm',
'hotel': 'oyo rooms'
}
print(appDict)
appDict.clear()
print(appDict)
In the above example, we have created a Python Dictionary and then clear all its items. See the below output.
You can also remove all elements from the dictionary by assigning an empty dictionary {}.
## app.py
appDict = {
'shopping': 'flipkart',
'transport': 'ola',
'banking': 'paytm',
'hotel': 'oyo rooms'
}
print(appDict)
appDict = {}
print(appDict)
We have assigned the empty dictionary.
#python #python dictionary clear