Python Function Reference

dict.clear

Remove all pairs from the dictionary.

Python 3.13
dict.clear()

Return value

Nothing (None); the dictionary is changed in place.

Examples

Clearing a dictionary

Python 3.13
a = {'x': 1, 'y': 2}
a.clear()
print(a)
{}

The dictionary itself is cleared, not the variable

Python 3.13
cache = {'user': 'Anna'}
same = cache
cache.clear()
print(same)
{}

Details

Unlike the assignment d = {}, clear() changes the dictionary itself, so the change is visible through all variables that refer to it.

See also