The clear() method of Python dictionary

Method Name:

clear

Method Signature:

clear()

Return Value:

None

Overview:

  • The clear() method removes all the elements from a Python dictionary object.
  • As all the elements of a dictionary are removed through the clear() method, the length of the dictionary becomes zero. 

Example:

# Example Python program that removes
# all the elements from a Python dictionary
expiredOrders = {1:"Expired",
                 10:"Expired",
                 14:"Expired",
                 15:"Expired"}
expiredOrders.clear()
print("Length of dictionary after removing all items:")
print(len(expiredOrders))

Output:

Length of dictionary after removing all items:
0

 


Copyright 2024 © pythontic.com