ReferenceSet methods

Python Set clear() Method

Short answer
set.clear() removes all elements from the set in place and returns None. The set object still exists afterwards, but it is empty.

Use clear() when you want to reuse the same set object, for example resetting a collection of seen items at the start of each round in a game. Because it works in place, every variable that refers to that set sees the change.

If you only need to remove some elements, use remove(), discard() or pop(). Understanding the difference between changing an object and rebinding a name is easier after reading about copying data.

Quick facts
Syntaxmy_set.clear()
ParametersNone
ReturnsNone (the set is emptied in place)
Time complexityO(n) in the number of elements

How set.clear() works

After the call the set has length 0 and prints as set(). Calling clear() on an already empty set is fine and does nothing.

Python
cart = {"pen", "book"}
cart.clear()
print(cart)
print(len(cart))

clear() vs assigning set() vs del

clear() empties the existing object. Writing s = set() makes the name point to a new empty set, and any other name that referred to the old one still sees the old contents. del s removes the name itself, so using it later raises NameError.

Python
a = {1, 2, 3}
b = a          # same set object
a = set()      # a now points to a new set
print(sorted(b))

c = {1, 2, 3}
d = c
c.clear()      # the shared object is emptied
print(d)

CodeEffect on the objectOther names for it
s.clear()Emptied in placeSee the empty set
s = set()Old object untouchedStill see old items
del sName removedStill see old items
s -= sEmptied in placeSee the empty set
s.pop() in a loopEmptied one by oneSee the empty set

Examples you can run

Press Run, then edit the code and run it again.

Reset a set between rounds

The same set object is reused for each round.

Reset a set between rounds
seen = set()
for round_words in [["a", "b"], ["c"]]:
    seen.clear()
    seen.update(round_words)
    print(sorted(seen))
Expected output['a', 'b']
['c']

clear() affects every reference

Both names point to one set, so clearing through one empties it for both.

clear() affects every reference
original = {1, 2, 3}
alias = original
original.clear()
print(alias)
print(alias is original)
Expected outputset()
True

Common mistakes

Using the return value

✗ Wrong

s = {1, 2}
s = s.clear()
print(s)
None

✓ Right

s = {1, 2}
s.clear()
print(s)

clear() returns None. Call it on its own line and keep using the same name.

Expecting clear() to delete the variable

✗ Wrong

s = {1, 2}
s.clear()
s.add(3)
del s
print(s)
NameError: name 's' is not defined

✓ Right

s = {1, 2}
s.clear()
s.add(3)
print(s)

clear() only removes the elements. The set is still usable afterwards. Use del only if you want the name gone.

Python Set clear() Method: questions people ask

How do I empty a set in Python?

Call my_set.clear(). It removes every element in place and keeps the same set object.

What is the difference between clear() and set()?

clear() empties the existing set, while s = set() binds the name to a brand-new empty set and leaves the old one unchanged for other references.

Does clear() return anything?

No. It returns None, so do not assign its result back to the variable.

Keep learning

Step-by-step lessons:
Python SetsCopying Lists and Dicts

Related:
Python Set remove() MethodPython Set discard() MethodPython Set pop() MethodPython Set add() Method

More in set methods:
Python Set add() MethodPython Set remove() MethodPython Set discard() MethodPython Set pop() MethodPython Set union() MethodPython Set intersection() MethodPython Set difference() MethodPython Set symmetric_difference() Method See all →