Python Dictionary clear()

Python Dictionary clear() method removes all the items from the dictionary. All data in the dictionary will be lost and the length of the dictionary becomes zero.

In this tutorial, we will learn how to clear all items of a dictionary using clear() method.

Syntax

The syntax of dict.clear() is

dict.clear()

clear() method returns None.

Example 1 – Python dict.clear()

In this example, we will take a dictionary with some initial key:value pairs, and clear all the items by calling clear() method on the dictionary.

Python Program

dictionary = {'a': 58, 'b': 22, 'c': 39}
dictionary.clear()
print(dictionary)
Try Online

Program Output

{}

Conclusion

In this Python Tutorial, we learned about the syntax and usage of Python Dictionary method dict.clear().