|
| Home |
|---|
The clear() method removes every key-value pair from a
dictionary, leaving it empty. Unlike deleting the dictionary entirely,
clear() keeps the variable itself intact — it's the standard
way to reset a dictionary for reuse without creating a brand-new object.
This page covers the exact syntax and return value of clear(),
when to reach for it over other removal methods, and a subtle but important
difference between clear() and simply reassigning a variable
to {} — one that matters as soon as your dictionary is shared
across multiple variables or functions.
dictionary.clear()
| Parameters | None — clear() accepts no arguments |
|---|---|
| Return value | None — the dictionary is emptied in place, nothing is returned |
Because clear() returns None, don't try to assign
its result — student = student.clear() would set
student to None, not an empty dictionary.
student = {
"name":"Alice",
"age":21,
"course":"Python"
}
student.clear()
print(student)
{}
Every key-value pair is removed. The dictionary variable still exists — it now just refers to an empty dictionary rather than being deleted.
A common pattern is emptying a dictionary immediately before populating it with fresh values — for example, resetting a record before loading updated data from a file or API.
data = {
"id":101,
"name":"Alice",
"course":"Python"
}
data.clear()
data["name"] = "Bob"
data["course"] = "Data Science"
print(data)
{'name': 'Bob', 'course': 'Data Science'}
Note the old "id" key is gone entirely after clear()
— it isn't carried over just because the dictionary is reused.
Two approaches look like they do the same thing:
dictionary.clear() # empties the SAME dictionary object
dictionary = {} # creates a NEW dictionary object, discards the old one
If only one variable ever refers to your dictionary, both produce identical visible results. But if the dictionary is shared — passed into a function, or referenced by more than one variable — the two behave very differently.
cache = {"a": 1, "b": 2}
backup_reference = cache # both variables point to the SAME dictionary
cache.clear()
print("cache:", cache)
print("backup_reference:", backup_reference)
cache: {}
backup_reference: {}
Because clear() empties the dictionary in place,
backup_reference — which points to that same object — is
emptied too. Now compare that to reassignment:
cache = {"a": 1, "b": 2}
backup_reference = cache # both variables point to the SAME dictionary
cache = {} # this makes "cache" point to a brand-new empty dictionary
print("cache:", cache)
print("backup_reference:", backup_reference)
cache: {}
backup_reference: {'a': 1, 'b': 2}
This time, backup_reference is untouched — cache = {}
only changed what the cache variable points to; it didn't
touch the original dictionary object at all.
| Approach | Original dictionary object | Other variables referencing it |
|---|---|---|
dictionary.clear() |
Emptied in place | Also see it as empty |
dictionary = {} |
Left untouched (just no longer referenced by this variable) | Still see the original data |
Use clear() when you need every reference to a dictionary to
see it become empty — for example, resetting a shared cache or configuration
object used across multiple parts of a program. Use reassignment when you
simply want a variable to start fresh and don't care about other references.
Because clear() mutates the dictionary in place rather than
creating a new one, calling it inside a function empties the caller's
original dictionary too — not just a local copy.
def reset_settings(settings):
settings.clear()
app_settings = {"theme": "dark", "volume": 80}
reset_settings(app_settings)
print(app_settings)
{}
app_settings is empty after the function call, even though the
function never explicitly returned anything — this is expected behavior
since dictionaries are mutable, but it can surprise beginners who assume a
function can't change a variable defined outside it.
clear() is one of several ways to remove dictionary data, but
the only one that empties the whole dictionary while keeping the variable
usable.
| Method | Removes | Dictionary variable afterward |
|---|---|---|
dictionary.clear() |
Every item | Still exists, now empty |
dictionary.pop(key) |
One item by key | Still exists, with remaining items |
del dictionary[key] |
One item by key | Still exists, with remaining items |
del dictionary |
Nothing (removes the variable itself) | No longer exists at all |
For removing individual items instead of clearing everything, see Remove Dictionary Items.
A practical reason to prefer clear() over reassignment is
performance and consistency when a dictionary is reused across many loop
iterations — clearing avoids repeatedly creating new dictionary objects.
batch_results = {}
datasets = [
["apple", "banana"],
["carrot", "date", "egg"]
]
for batch in datasets:
batch_results.clear() # reset for this batch, reuse the same object
for i, item in enumerate(batch):
batch_results[i] = item
print(batch_results)
{0: 'apple', 1: 'banana'}
{0: 'carrot', 1: 'date', 2: 'egg'}
The same dictionary object is reused and reset each iteration with
clear(), rather than creating a new empty dictionary on every
pass through the loop.
dictionary.clear() and dictionary = {} always behave identically — they differ when other variables reference the same dictionaryclear() to a variable, forgetting it returns Noneclear() — it doesn't, only its contents doclear() inside a function and being surprised the caller's original dictionary is also emptiedclear() on a non-dictionary object, such as a list, which has its own separate clear() method with the same name but different underlying object
clear() returns None. The dictionary is
emptied in place, so there's nothing meaningful to assign from the
call itself.
Only if nothing else references the dictionary. clear()
empties the existing object in place, so any other variable pointing
to it also sees it become empty. dictionary = {} instead
creates a brand-new dictionary and only changes what that one variable
points to — other references still see the original data.
No — the variable still exists and still refers to a dictionary,
just an empty one. To remove the variable entirely, use
del dictionary instead.
No — clear() takes no parameters. It always removes
every item; there's no way to clear only some items with this method.
To remove specific items, use pop() or del.
Struggling with Python syntax, assignments or debugging errors? Learning alone can be frustrating.
Get 1-on-1 Python tutoring and learn faster with clear explanations, real examples and live help.
Home Tutorial Online Live Trainings Contact Us © 2026 All rights reserved by www.PythonTutor.net