Python 3 reference · Dictionary Methods

Python Dictionary clear() Method

Remove every key and value from the dictionary in place, leaving it empty. Other references see the change too.

Python 3 Since 3.0

Syntax

dict.clear()

Return value

None.

Examples

Empty a dictionaryTry it yourself
cache = {"a": 1, "b": 2}
cache.clear()
print(cache)

Output

{}
Other references see it tooTry it yourself
settings = {"x": 1}
alias = settings
settings.clear()
print(alias)  # same object, also empty

Output

{}

Version history

VersionChange
Python 3.0Added