Tutorial Study Image

Python clear()

The clear()  function in python is used to delete all the elements from the dictionary.After this method calling the dictionary gets initialized to an empty dictionary.


dict.clear() 
 

clear() Parameters:

clear() method doesn't take any parameters.If we call clear function on a dictionary reference, the actual dictionary content is removed, and hence all references referring to the actual dictionary also become empty.

clear() Return Value

clear() method doesn't return any value (returns None).

Examples of clear() method in Python

Example 1: How clear() method works for dictionaries?


dict = {5: "five", 6: "six"}

dict.clear()
print('dic =', dict)
 

Output:


dic = {}

Example 2: How clear() method works for dictionaries?


my_namelist = [12, 'Siya', 'Tiya', 14, 'Riya', 12, 'Riya']

#Using clear() method
element = my_namelist.clear()
print(element)
print(my_namelist)
 

Output:


None
[]