set.add
Add an item to the set.
Python 3.13
set.add(elem)Parameters
- elemHashable
- The item to add.
Return value
Nothing (None); the set is changed in place.
Examples
Adding an item
Python 3.13
s = {1, 2}
s.add(3)
print(s){1, 2, 3}A duplicate is not added
Python 3.13
s = {1, 2}
s.add(2)
print(s){1, 2}Details
If the item is already there, the set does not change. Lists and dictionaries cannot be added because they are unhashable; this raises TypeError.
