VOOZH about

URL: https://www.geeksforgeeks.org/python/python-dictionary-clear/

⇱ Python Dictionary clear() - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Python Dictionary clear()

Last Updated : 25 Feb, 2025

clear() method in Python is used to remove all items (key-value pairs) from a dictionary. After calling this method, the dictionary will become empty and its length will be 0. This method is part of the built-in dictionary operations in Python.

Example:


Output
{}

Explanation: In this example, after calling clear(), the dictionary d becomes empty.

Note: clear() method is safe to use and won't raise errors, even if the dictionary is already empty. It simply clears all items.

clear() syntax

dict.clear()

Here, dict is the dictionary on which the clear() method is called.

Parameters:

  • None: clear() method does not take any parameters.

Returns:

  • This method does not return anything. It modifies the dictionary in place, removing all items from it.

clear() examples

Example 1: clear() on an already empty dictionary.


Output
{}

Explanation: In this case, the dictionary was already empty, but calling clear() does not change its state.

Example 2: Clearing a dictionary after operations.


Output
{}

Explanation: In this case, the dictionary d is updated before calling clear(). After calling the method, the dictionary becomes empty.

Comment
Article Tags: