The clear()
method in Python is a dictionary method that removes all items from a dictionary. It does not return any value and modifies the original dictionary in place.
Parameter Values
This function does not accept any parameters.Return Values
The clear()
method from Dictionary returns None
.
How to Use clear()
in Python
Example 1:
The clear()
method removes all items from the dictionary.
dict1 = {'a': 1, 'b': 2, 'c': 3}
dict1.clear()
print(dict1) # Output: {}
Example 2:
If you call clear()
on an empty dictionary, it will remain empty.
dict2 = {}
dict2.clear()
print(dict2) # Output: {}
Example 3:
The clear()
method does not return any value (returns None
).
dict3 = {'x': 10, 'y': 20, 'z': 30}
result = dict3.clear()
print(result) # Output: None