all Python 3Sets (set)function
set.add
adding
Add an element to the set in place; duplicates are ignored.
Syntax
s.add(elem)
It returns None, so s = s.add(3) quietly turns your set into None — a classic slip. The element must be hashable: a list raises TypeError (unhashable type), so pass a tuple or a frozenset. Membership is decided by hash and ==, which makes 1, 1.0 and True the same element, and adding a duplicate leaves the stored object untouched.
Examples
s = {1, 2}
s.add(3)
print(sorted(s)) # → [1, 2, 3]
s.add(2)
print(sorted(s)) # → [1, 2, 3]
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy