Join Regular Classroom : Visit ClassroomTech

clear() in Python Dictionary

It is a method in python that clears all the content of the dictionary. It empties the existing dictionary. It modifies the dictionary. And it doesn’t return a value.

Syntax:

dictionary.clear()

Don’t know what is len in Python? Read: len() in Python Dictionary

Example 1:

s = {0:"CodeWindow", 1:"India", 2:"News"}
s.clear()
print(s)

Output:

{}

Explanation:

Here it clears the dictionary and leaves an empty dictionary.

Example 2:

s = {"site":"CodeWindow", "country":"India", "type":"News", "platform":"IT"}
s.clear()
print(s)

Output:

{}

Explanation:

Here it clears the dictionary and leaves an empty dictionary.


You Missed

Also Checkout