In this section we will learn what the Dictionary clear() method is and how to use it in Python.
What is Dictionary clear() Method in Python?
The Python Dictionary clear() method is used to clear the entire elements of a Dictionary.
That means after calling this method, the size of the dictionary becomes 0.
Python Dictionary clear() Method Syntax:
dictionary.clear()
Dictionary clear() Method Parameter:
The method takes no arguments.
Dictionary clear() Method Return Value
The method does not return a value either.
Example: using python dictionary clear() method
dictionary = { "name":"John", "lastName":"Doe" } print("The size of the dictionary before calling the clear() method:") print(len(dictionary)) dictionary.clear() print("The size of the dictionary after calling the clear() method: ") print(len(dictionary))
Output:
The size of the dictionary before calling the clear() method: 2 The size of the dictionary after calling the clear() method: 0