Python Set clear() Method
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.
| Syntax | my_set.clear() |
|---|---|
| Parameters | None |
| Returns | None (the set is emptied in place) |
| Time complexity | O(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.
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.
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)| Code | Effect on the object | Other names for it |
|---|---|---|
s.clear() | Emptied in place | See the empty set |
s = set() | Old object untouched | Still see old items |
del s | Name removed | Still see old items |
s -= s | Emptied in place | See the empty set |
s.pop() in a loop | Emptied one by one | See 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.
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.
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)✓ 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)✓ 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 →