Python dictionary clear() Method



The Python dictionary clear() method is used to remove all the elements (key-value pairs) present in the dictionary at once. Hence it retrieves an empty dictionary.

When there are too many elements in a dictionary, it takes a long time to remove each element one by one. Instead, use the clear() method to remove all theĀ elements at once.

Syntax

Following is the syntax of the Python dictionary clear() method −

dict.clear()

Parameters

This method does not accept any parameter.

Return Value

This method does not return any value.

Example

The following example shows the usage of Python dictionary clear() method. Here we are creating a dictionary 'Animal'. Then all the elements present in the dictionary 'Animal' is removed using clear() method.

Animal = {"Name":"Lion","Kingdom":"Animalia","Class":"Mammalia","Order":"Carnivora"}
print("Elements of the dictionary before removing elements are: ", str(Animal))
res = Animal.clear()
print("Elements of the dictionary after removing elements are: ", res)

When we run above program, it produces following result −

Elements of the dictionary before removing elements are:  {'Name': 'Lion', 'Kingdom': 'Animalia', 'Class': 'Mammalia', 'Order': 'Carnivora'}
Elements of the dictionary after removing elements are:  None

Example

In the code below a dictionary 'dict' is created. Then all the elements present in the dictionary 'dict' is removed using the clear() method. Here, we retrieve the length of the dictionary before and after removing its elements.

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

Following is an output of the above code −

Start Len : 2
End Len : 0

Example

The code below shows the difference between the clear() method and assigning {} to an existing dictionary. A new empty dictionary is created and assigned to the given reference by assigning {} to the dictionary. Whereas by invoking clear() method on the dictionary reference, the elements of the actual dictionary is removed. So, all the references which refers to the dictionary becomes empty.

dict_1 = {"Animal": "Lion", "Order": "Carnivora"}
dict_2 = dict_1
# Using clear() method
dict_1.clear()
print('The first dictionary dict_1 after removing items using clear() method: ', dict_1)
print('The second dictionary dict_2 after removing items using clear() method: ', dict_2)

dict_1 = {"Player": "Sachin", "Sports": "Cricket"}
dict_2 = dict_1
# Assigning {}
dict_1 = {}
print('The first dictionary dict_1 after removing items by assigning {}:', dict_1)
print('The second dictionary dict_2 after removing items by assigning {}: ', dict_2)

Output of the above code is as follows −

The first dictionary dict_1 after removing items using clear() method:  {}
The second dictionary dict_2 after removing items using clear() method:  {}
The first dictionary dict_1 after removing items by assigning {}: {}
The second dictionary dict_2 after removing items by assigning {}:  {'Player': 'Sachin', 'Sports': 'Cricket'}
python_dictionary.htm
Advertisements