Python Dictionary clear() Method
dict.clear() removes all key-value pairs from a dictionary in place, leaving it empty, and returns None. Every variable that refers to that dictionary sees it become empty.clear() is the simplest way to reset a dictionary while keeping the same object, for example a cache or a settings store that other parts of your program already hold a reference to. It is one of the basic dictionary methods.
The key point is in place: clear() empties the existing object instead of creating a new one. That is what separates it from d = {}, a difference that causes real bugs when two names point at the same dictionary.
| Syntax | my_dict.clear() |
|---|---|
| Parameters | None |
| Returns | None |
| Effect | Removes all items; the dictionary object itself remains |
| Time complexity | O(n) |
What dict.clear() does
After clear(), len(d) is 0 and d prints as {}. Calling it on an already empty dictionary is fine and does nothing. It never raises an error for an empty dictionary, unlike popitem().
cart = {"pen": 2, "book": 1}
print(cart.clear())
print(cart)
print(len(cart))The first line prints None because clear() returns nothing useful.
clear() vs assigning {} vs del
Assigning d = {} rebinds the name to a new empty dictionary. Other names that referred to the old dictionary still see the old contents. d.clear() empties the shared object, so every name sees the change. del d removes the name itself, not the contents, and using d afterwards raises NameError.
Remember that b = a does not copy a dictionary; both names refer to the same object. If you need an independent copy before clearing, use dict.copy().
| Code | Same dictionary object kept? | Other references see an empty dict? | Name still usable? |
|---|---|---|---|
d.clear() | Yes | Yes | Yes |
d = {} | No, a new one is made | No, they keep the old data | Yes |
del d | Name is removed | Only if no other name holds it | No |
When to use clear() and when to remove single keys
Use clear() to reset caches, state holders or loop accumulators that you reuse. To remove a single entry, use pop() or del d[key] instead. To keep only some entries, a dictionary comprehension building a new dictionary is usually clearer than clearing and re-adding.
Examples you can run
Press Run, then edit the code and run it again.
Reset a dictionary after use
We reuse the same dictionary for every order by clearing it at the end of each loop.
orders = [["pen", "book"], ["ruler"]]
cart = {}
for order in orders:
for item in order:
cart[item] = cart.get(item, 0) + 1
print(len(cart), "item types")
cart.clear()
print(cart)Expected output2 item types
1 item types
{}
clear() changes every reference
Both names point at the same dictionary, so clearing through one empties both.
a = {"x": 1}
b = a
a.clear()
print(b)Expected output{}
Assigning {} leaves other references alone
a = {} only rebinds the name a; b still holds the old data.
a = {"x": 1}
b = a
a = {}
print(a)
print(b)Expected output{}
{'x': 1}
Common mistakes
Using the return value of clear()
✗ Wrong
d = {"a": 1}
d = d.clear()
print(d)✓ Right
d = {"a": 1}
d.clear()
print(d)clear() returns None and empties the dictionary itself. Call it without assigning.
Clearing a dictionary you meant to keep
✗ Wrong
original = {"a": 1}
backup = original
original.clear()
print(backup)✓ Right
original = {"a": 1}
backup = original.copy()
original.clear()
print(backup)Make a real copy first with original.copy().
Python Dictionary clear() Method: questions people ask
How do I empty a dictionary in Python?
Call my_dict.clear(). It removes all items in place. Assigning my_dict = {} also gives an empty dictionary but creates a new object.
What is the difference between clear() and del for a dictionary?
clear() removes the items but keeps the dictionary, while del my_dict deletes the variable name itself.
Does clear() return anything?
No. It returns None.
Keep learning
Step-by-step lessons:
Python DictionariesPython Dictionary MethodsCopying Lists and Dicts
Related:
Python Dictionary copy() MethodPython Dictionary pop() MethodPython Dictionary popitem() MethodPython Dictionary update() Method
More in dictionary methods:
Python Dictionary get() MethodPython Dictionary items() MethodPython Dictionary keys() MethodPython Dictionary values() MethodPython Dictionary update() MethodPython Dictionary pop() MethodPython Dictionary popitem() MethodPython Dictionary setdefault() Method See all →