Home »
Python
Python Dictionary clear() Method with Example
Python Dictionary clear() Method: Here, we are going to learn how to clear all elements of a dictionary using clear() method?
Submitted by IncludeHelp, on November 25, 2019
Dictionary clear() Method
clear() method is used to clear all elements of the dictionary.
Syntax:
dictionary_name.clear()
Parameter(s):
- It does not accept any parameter.
Return value:
The return type of this method is <class 'NoneType'>, it returns nothing.
Example:
# Python Dictionary clear() Method with Example
# 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...
{}
ADVERTISEMENT
ADVERTISEMENT