Python set.clear() Method

Definition
clear() removes every item from a set and leaves it empty. The set is changed in place: the same set object stays alive, just with no items.
Every name that refers to that set sees the change. Assigning s = set() is different: it makes a new empty set and only rebinds the name s.
set.clear() is an instance method of the built-in set type: call it on a set with dot notation, such as cache.clear(...). No import is needed.
Syntax
Call it with no arguments, on its own line, because the return value is always None.
| Call | What it does |
|---|
{1, 2, 3}.clear() | Returns None; the set becomes set(). |
set().clear() | Returns None; the set becomes set(). Clearing an empty set is allowed and does nothing. |
{1, 2}.clear(1) | Raises TypeError: clear() takes no arguments. |
clear() never raises for an empty set. To remove one item use remove() or discard(); to keep the old items before clearing, take a copy() first.
Method signatures
set.clear()
# returns: None; the set is now empty
Parameters
This method takes no parameters: clear(x) raises TypeError.
Return Value
| Return Value | Description |
|---|
None | Always None. The set itself is now empty, so len(s) is 0. |
Python Examples
Example 1 — Empty a Shopping Cart After Checkout
Clear the cart and look at what the method returns and what the set looks like afterwards.
cart = {"pen", "notebook", "stapler"}
print(len(cart))
print(cart.clear())
print(cart)
print(len(cart), "pen" in cart)
How It Works
- Before. The cart holds three different products, so the size is 3.
- No return value.
clear() empties the set and returns None, which is printed. - An empty set prints as set(). Python writes
set() because {} would mean an empty dict. - Nothing is left. The size is 0 and a membership test for "pen" is
False.
Example 2 — See Why clear() Beats Rebinding
Two names share one set. Reassign one name, then clear the shared set, and compare.
live = {"a", "b", "c"}
view = live
live = set()
print(sorted(view))
live = view
live.clear()
print(sorted(view), sorted(live))
How It Works
- Two names, one set.
view = live does not copy anything; both names point to the same set. - Rebinding leaves the old set alone.
live = set() gives live a new empty set, while view still sees "a", "b", and "c". - Clearing changes the shared object. After
live = view, calling live.clear() empties the one set both names use. - Both names agree. Both sorted lists print as
[].
Example 3 — Clear a Page Cache but Keep a Backup
Copy the set, clear the original, and rebuild it with one item.
cache = {"home", "about", "contact"}
snapshot = cache.copy()
cache.clear()
print(len(cache), len(snapshot))
cache.add("home")
print(sorted(cache))
print(sorted(snapshot))
0 3
['home']
['about', 'contact', 'home']
How It Works
- Copy first.
cache.copy() makes a separate set, so the next step cannot affect it. - Only the original is emptied. The cache has 0 items while the snapshot still has 3.
- A cleared set is reusable. Adding "home" starts the cache again, and the same set object is used.
- The backup is intact. The snapshot keeps all three pages, printed in alphabetical order.
Frequently Asked Questions
It removes every item from the set in place and returns None. The set object still exists, and you can add items to it again.
clear() empties the existing set, so every name pointing to it sees the change. s = set() makes a new empty set and only rebinds the name s.
No. Clearing an empty set simply does nothing and returns None.
Make a copy first with backup = s.copy() or set(s). A plain backup = s only creates a second name for the same set.
clear() empties the set but keeps the variable. del s deletes the variable name, so using s afterwards raises NameError.
Next: set.copy()
Return a new set with the same items.
copy() →About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
11 people found this page helpful