Python Dictionary clear()

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

Example


clear() Syntax

The syntax of the clear() method is:

dictionary.clear()

Here, clear() removes all the items present in the dictionary.


clear() Parameters

The clear() method doesn't take any parameters.


clear() Return Value

The clear() method doesn't return any value.


Example 1: Python Dictionary clear()

Output

Dictionary before clear(): {'New York': 18, 'Texas': 26}
Dictionary after clear(): {}

In the above example, we have used the clear() method to remove all the items from the dictionary cityTemperature.

Here, the method removes every item from {"New York": 18, "Texas": 26} and returns an empty dictionary {}.