The clear()
method removes all items from the dictionary.
Example
# dictionary numbers = {1: "one", 2: "two"} # removes all the items from the dictionary numbers.clear() print(numbers) # Output: {}
clear() Syntax
The syntax of the clear()
method is:
dictionary.clear()
Here, clear()
removes all the items present in the dictionary.
clear() Parameters
The clear()
method doesn't take any parameters.
clear() Return Value
The clear()
method doesn't return any value.
Example 1: Python Dictionary clear()
cityTemperature = {"New York": 18, "Texas": 26} print("Dictionary before clear():", cityTemperature) # removes all the items from the dictionary cityTemperature.clear() print("Dictionary after clear():", cityTemperature)
Output
Dictionary before clear(): {'New York': 18, 'Texas': 26} Dictionary after clear(): {}
In the above example, we have used the clear()
method to remove all the items from the dictionary cityTemperature.
Here, the method removes every item from {"New York": 18, "Texas": 26}
and returns an empty dictionary {}
.
Also Read: