Python set.clear() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
set 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.

python
s.clear()
CallWhat 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
python
set.clear()
# returns: None; the set is now empty

Parameters

This method takes no parameters: clear(x) raises TypeError.

Return Value

Return ValueDescription
NoneAlways 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.

python
cart = {"pen", "notebook", "stapler"}
print(len(cart))

print(cart.clear())
print(cart)
print(len(cart), "pen" in cart)

How It Works

  1. Before. The cart holds three different products, so the size is 3.
  2. No return value. clear() empties the set and returns None, which is printed.
  3. An empty set prints as set(). Python writes set() because {} would mean an empty dict.
  4. 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.

python
live = {"a", "b", "c"}
view = live

live = set()
print(sorted(view))

live = view
live.clear()
print(sorted(view), sorted(live))

How It Works

  1. Two names, one set. view = live does not copy anything; both names point to the same set.
  2. Rebinding leaves the old set alone. live = set() gives live a new empty set, while view still sees "a", "b", and "c".
  3. Clearing changes the shared object. After live = view, calling live.clear() empties the one set both names use.
  4. 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.

python
cache = {"home", "about", "contact"}
snapshot = cache.copy()

cache.clear()
print(len(cache), len(snapshot))

cache.add("home")
print(sorted(cache))
print(sorted(snapshot))

How It Works

  1. Copy first. cache.copy() makes a separate set, so the next step cannot affect it.
  2. Only the original is emptied. The cache has 0 items while the snapshot still has 3.
  3. A cleared set is reusable. Adding "home" starts the cache again, and the same set object is used.
  4. 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

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.

11 people found this page helpful