![]() |
VOOZH | about |
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:
{}
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.
dict.clear()
Here, dict is the dictionary on which the clear() method is called.
Parameters:
Returns:
Example 1: clear() on an already empty dictionary.
{}
Explanation: In this case, the dictionary was already empty, but calling clear() does not change its state.
Example 2: Clearing a dictionary after operations.
{}
Explanation: In this case, the dictionary d is updated before calling clear(). After calling the method, the dictionary becomes empty.