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
cache = {"a": 1, "b": 2}
cache.clear()
print(cache)
Output
{}settings = {"x": 1}
alias = settings
settings.clear()
print(alias) # same object, also empty
Output
{}Version history
| Version | Change |
|---|---|
| Python 3.0 | Added |
Related
dict.pop()· Remove a key and return its value. Returns the default if given and the key is missing; otherwise raises KeyError.dict.popitem()· Remove and return the last inserted (key, value) pair (LIFO order). Raises KeyError if the dict is empty.list.clear()· Remove every item from the list in place, leaving an empty list. Other references see the change too.- Python Dictionaries · tutorial