Home »
Python
Python Dictionary clear() Method (with Examples)
Python Dictionary clear() Method: In this tutorial, we will learn about the clear() method of a dictionary with its usage, syntax, parameters, return type, and examples.
By IncludeHelp Last updated : June 12, 2023
Python Dictionary clear() Method
The clear() is an inbuilt method of dict class that is used to clear the dictionary i.e., to remove all elements of the dictionary. The method is called with this dictionary and returns none.
Syntax
The following is the syntax of clear() method:
dictionary_name.clear()
Parameter(s):
The following are the parameter(s):
Return Value
The return type of this method is <class 'NoneType'>, it returns nothing.
Example 1: Use of Dictionary clear() Method
# dictionary declaration
student = {
"roll_no": 101,
"name": "Shivang",
"course": "B.Tech",
"per" : 98.5
}
# printing dictionary
print("data before clearing...")
print(student)
# clearing dictionary
student.clear()
# printing dictionary after clear()
print("data after clearing...")
print(student)
Output
data before clearing...
{'course': 'B.Tech', 'roll_no': 101, 'per': 98.5, 'name': 'Shivang'}
data after clearing...
{}
Example 2: Use of Dictionary clear() Method
# dictionary declaration
data = {"a": 1, "c": 3, "b": 5, "d": 4}
# printing dictionary
print("Before clearing...")
print(data)
# clearing dictionary
data.clear()
# printing dictionary after clear()
print("After clearing...")
print(data)
Output
Before clearing...
{'a': 1, 'c': 3, 'b': 5, 'd': 4}
After clearing...
{}