Post

Python Dictionary clear()

The clear() method removes all the items from a dictionary.

Python Dictionary clear()

The syntax of clear() method is:

1
2
dictionary.clear()

clear() Parameters

The clear() method does not take any parameter as argument.

Let’s see an example of the dictionary clear() method.

Example 1: How to use the clear() method in python dictionaries?

1
2
3
4
5
6
7
8
cars = {1 : "TATA",2 : "BMW", 3 : "TOYOTA"}
print("Dictionary is:",cars)

# clearing dictionary using clear() method
cars.clear()

print("Dictionary is:",cars)

Output:

1
2
3
Dictionary is: {1: 'TATA', 2: 'BMW', 3: 'TOYOTA'}
Dictionary is: {}

Here we can see that the clear() method has completely removed all the items from a dictionary.

Rules of clear()

There are no such rules to use clear() method.

This post is licensed under CC BY 4.0 by the author.