Python Tutoring Online
 Home

Python Dictionary clear() Method

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.

Need Help Learning Python?

If you are learning Python and finding programming concepts difficult, working with an experienced Python tutor can help you progress faster.

One-to-one Python tutoring sessions provide practical coding exercises, clear explanations and help with assignments, projects and debugging.

Get Python Tutoring Help

Syntax

Python
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.

Example 1: Remove All Dictionary Items

Python
student = {
"name":"Alice",
"age":21,
"course":"Python"
}

student.clear()

print(student)

Output


{}

Every key-value pair is removed. The dictionary variable still exists — it now just refers to an empty dictionary rather than being deleted.

Example 2: Using clear() Before Loading New Data

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.

Python
data = {
"id":101,
"name":"Alice",
"course":"Python"
}

data.clear()

data["name"] = "Bob"
data["course"] = "Data Science"

print(data)

Output


{'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.

clear() vs Reassigning to {} — They're Not Always the Same

Two approaches look like they do the same thing:

Python
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.

Python
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)

Output


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:

Python
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)

Output


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.

clear() Inside a Function: Affects the Caller's Dictionary

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.

Python
def reset_settings(settings):
    settings.clear()

app_settings = {"theme": "dark", "volume": 80}

reset_settings(app_settings)

print(app_settings)

Output


{}

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() vs Other Removal Methods

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.

Real-World Use Case: Resetting a Dictionary Reused Across Loop Iterations

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.

Python
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)

Output


{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.

Common Beginner Mistakes

Frequently Asked Questions

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.

Related Python Dictionary Tutorials




Stuck Learning Python?

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.

    Get Python Tutoring

PythonTutor.net

Home    Tutorial     Online Live Trainings    Contact Us           © 2026 All rights reserved by www.PythonTutor.net