Python dict.clear() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
dict method

Definition

clear() deletes every key/value pair, leaving an empty dict {}. The dict object itself survives, so the change is in place.

Any other variable, list, or dict that refers to the same object sees the empty dict too. That is the key difference from writing d = {}, which only points one name at a new dict.

dict.clear() is an instance method of the built-in dict type: call it on a dictionary with dot notation, such as cart.clear(...). No import is needed.

Syntax

Call it with no arguments; there is nothing useful to store from the result.

python
d.clear()
CallWhat it does
cart.clear()Empties cart; every name bound to the same dict now sees {}.
{"a": 1, "b": 2}.clear()Returns None; the dict becomes {}.
cart = {}Not the same: binds the name to a new dict, and other references keep the old items.

del d is different again: it deletes the name d, not the items, and the dict lives on if something else refers to it.

Method signatures
python
dict.clear()
# returns: None; the dict is now empty

Parameters

clear() takes no parameters.

Return Value

Return ValueDescription
NoneAlways None. The same dict object is now empty.

Python Examples

Example 1 — Empty a Shopping Cart After Checkout

Count the items, clear the cart, and check a second name that refers to the same dict.

python
cart = {"apple": 3, "bread": 1, "milk": 2}
saved = cart

total_items = sum(cart.values())
print("Checked out", total_items, "items")
result = cart.clear()
print(result)
print(cart, len(cart))
print(saved)
print(saved is cart)

How It Works

  1. Count first. sum() over the quantities gives 3 + 1 + 2 = 6 before anything is removed.
  2. Nothing returned. clear() returns None, so storing its result is pointless.
  3. Empty, same object. The cart prints as {} with length 0.
  4. Aliases see it. saved is not a copy but a second name for the cart, so it is empty too, and is confirms one object.

Example 2 — Compare clear() with Rebinding to {}

A login registry holds a reference to the sessions dict. Rebinding and clearing affect it very differently.

python
sessions = {"u17": "alice", "u42": "bob"}
registry = {"active": sessions}

sessions = {}
print(sessions)
print(registry["active"])

sessions = registry["active"]
sessions.clear()
print(registry["active"])
print(registry)

How It Works

  1. Rebinding. sessions = {} makes the name point at a new, empty dict.
  2. Old data lives on. The registry still holds the original dict, so both users are still listed under "active".
  3. Clearing the real one. After pointing sessions back at the stored dict, clear() empties that object.
  4. Seen through the container. The registry now shows {'active': {}}, because it holds the very dict that was cleared.

Example 3 — Invalidate a Currency Cache After Rates Change

A function caches conversions in a module-level dict. When a rate changes, clearing the cache forces fresh results.

python
rates = {"USD": 1.0, "EUR": 0.5}
cache = {}


def convert(amount, currency):
    key = (amount, currency)
    if key not in cache:
        cache[key] = amount * rates[currency]
    return cache[key]


print(convert(10, "EUR"), len(cache))
rates["EUR"] = 0.25
print(convert(10, "EUR"), "(stale)")
cache.clear()
print(convert(10, "EUR"), len(cache))

How It Works

  1. Tuple keys. Each cache entry is keyed by the hashable tuple (amount, currency).
  2. First call computes. 10 times 0.5 is 5.0, stored as the cache's single entry.
  3. Stale hit. After the rate drops to 0.25, the cached 5.0 is still returned, because the key has not changed.
  4. Clear to refresh. cache.clear() empties the same dict the function uses, so the next call recomputes 2.5 and stores it again.

Frequently Asked Questions

None. It empties the dict in place, so there is nothing to assign.
d.clear() empties the existing object, so every reference to it sees an empty dict. d = {} only makes the name d point to a new dict; other references keep the old items.
No. The dict object still exists and is simply empty. del d removes the name d instead.
Copy it first with backup = d.copy(), then call d.clear(). The backup keeps the old items.
Not on the dict you are looping over: the size change raises RuntimeError on the next loop step. Clear it after the loop ends.

Next: dict.copy()

Return a shallow copy: a new dict that still shares nested lists and dicts.

copy() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful