Python Function Reference

set.clear

Remove all items from the set.

Python 3.13
set.clear()

Return value

Nothing (None); the set is changed in place.

Examples

Clearing a set

Python 3.13
s = {1, 2, 3}
s.clear()
print(s)
set()

The set itself is cleared, not the variable

Python 3.13
s = {1, 2}
same = s
s.clear()
print(same, len(same))
set() 0

Details

An empty set is printed as set() because {} means an empty dictionary. The change is visible through all variables that refer to this set.

See also

Where to learn