Dictionary clear() Method



Description

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

Syntax

Following is the syntax for clear() method −

dict.clear()

Parameters

NA

Return Value

This method does not return any value.

Example

The following example shows the usage of clear() method.

dict = {'Name': 'Zara', 'Age': 7};
print ("Start Len : %d" % len(dict))
dict.clear()
print ("End Len : %d" % len(dict))

When we run above program, it produces the following output

Start Len : 2
End Len : 0
python_dictionary_methods.htm
Advertisements