Python dictionary - clear() : method

Python Dictionary : clear()

  1. In order to remove the all items from the dictionary we use clear().
  2. It does not take any parameter
  3. Method does not return any value.

Syntax

Below syntax is used for clear() method -

dict.clear()

Python Dictionary : clear() Example

#!/usr/bin/python
dict = {'Word1': 'Meaning1', 'Word2': 'Meaning2'};
print ("Dictionary Length : %d" %  len(dict))
dict.clear()
print ("Dictionary Length : %d" %  len(dict))

Output :

After running the program we will get below output -

>>> 
Dictionary Length : 2
Dictionary Length : 0
>>>