Python Dictionary clear() Method
dict.clear() removes all items from a dictionary.
Use it when you want to empty a dictionary without replacing the variable with a new dictionary. This method changes the original dictionary in place, which means the same dictionary object is kept, but all key-value pairs are removed.
Quick example
data = {"a": 1, "b": 2}
data.clear()
print(data) # {}
clear() removes all items from the same dictionary object.
What clear() does
The clear() method:
- Removes all key-value pairs from a dictionary
- Changes the original dictionary in place
- Returns
None - Leaves you with an empty dictionary:
{}
This is one of the standard ways to remove dictionary contents without deleting the variable itself.
If you are new to dictionaries, see Python dictionaries explained.
Basic syntax
my_dict.clear()
Important points:
clear()takes no arguments- You call it on a dictionary object
- It empties that dictionary
Simple example
Here is a basic example:
user = {
"name": "Maya",
"age": 25,
"city": "Berlin"
}
print("Before:", user)
user.clear()
print("After:", user)
Expected output:
Before: {'name': 'Maya', 'age': 25, 'city': 'Berlin'}
After: {}
What happens here:
userstarts with three itemsuser.clear()removes them all- The dictionary still exists, but it is now empty
What clear() returns
clear() returns None.
This is a very common beginner mistake:
my_dict = {"x": 1, "y": 2}
result = my_dict.clear()
print(result)
print(my_dict)
Output:
None
{}
Common mistake
Do not do this:
my_dict = {"x": 1, "y": 2}
my_dict = my_dict.clear()
print(my_dict)
Output:
None
Why this happens:
my_dict.clear()empties the dictionary- The method returns
None - Then
my_dict = ...storesNonein the variable
If you want to keep using the dictionary, just call clear() by itself:
my_dict = {"x": 1, "y": 2}
my_dict.clear()
print(my_dict) # {}
clear() vs creating a new empty dictionary
These two lines may look similar, but they do different things:
my_dict.clear()
my_dict = {}
clear()
- Empties the existing dictionary object
- Keeps the same object in memory
- Affects all variables that refer to that same dictionary
my_dict = {}
- Creates a new empty dictionary object
- Reassigns the variable to that new object
- Other variables pointing to the old dictionary are unchanged
This difference matters when you are working with shared references. It is also related to mutable vs immutable types in Python.
Shared reference example
If two variables point to the same dictionary, clear() affects both because both names refer to the same object.
original = {"a": 1, "b": 2}
other_ref = original
original.clear()
print("original:", original)
print("other_ref:", other_ref)
print(original is other_ref)
Output:
original: {}
other_ref: {}
True
This shows:
originalandother_refrefer to the same dictionaryclear()changes that dictionary in place- Both variables now show an empty dictionary
Now compare that with reassignment:
original = {"a": 1, "b": 2}
other_ref = original
original = {}
print("original:", original)
print("other_ref:", other_ref)
print(original is other_ref)
Output:
original: {}
other_ref: {'a': 1, 'b': 2}
False
Here:
original = {}creates a new dictionaryother_refstill points to the old one
If you want to make a separate dictionary before changing it, see dict.copy().
When to use clear()
clear() is useful when you want to:
- Reset stored data before reusing a dictionary
- Empty a cache or temporary mapping
- Remove all items without deleting the variable
Example:
session_data = {
"user_id": 101,
"theme": "dark",
"logged_in": True
}
# Reset for a new session
session_data.clear()
print(session_data) # {}
If you only want to remove one item, use a more specific method like dict.pop() or read how to remove a key from a dictionary in Python.
If you want to remove the last inserted item, see dict.popitem().
Common mistakes
Here are the most common problems beginners run into with clear():
- Assigning the result of
clear()to a variable - Expecting
clear()to return a new dictionary - Not realizing the dictionary is changed in place
- Clearing a dictionary that is shared by another variable
If something looks wrong, these quick checks can help:
print(my_dict)
print(type(my_dict))
print(id(my_dict))
print(other_ref is my_dict)
What these show:
print(my_dict)shows the current contentsprint(type(my_dict))confirms it is still a dictionaryprint(id(my_dict))helps you check whether it is the same object as beforeprint(other_ref is my_dict)tells you whether two variables point to the same object
FAQ
Does clear() delete the dictionary?
No. It keeps the dictionary object but removes all items from it.
What does dict.clear() return?
It returns None.
What is the difference between clear() and {}?
clear() empties the same dictionary object. Assigning {} creates a new empty dictionary.
Does clear() work on nested dictionaries?
It removes the top-level items. If nested dictionaries were inside it, those references are removed from this dictionary.