Python Set add() Method
set.add() adds one item to a set.
Use it when you want to put a single value into an existing set. If that value is not already present, Python adds it. If it is already in the set, nothing changes.
Quick example
numbers = {1, 2, 3}
numbers.add(4)
print(numbers)
Output:
{1, 2, 3, 4}
Use add() to put one value into a set. If the value is already in the set, nothing changes.
What set.add() does
set.add() is a built-in set method that:
- Adds one item to a set
- Changes the original set in place
- Does not return a new set
- Leaves the set unchanged if the item already exists
If you are new to sets, see Python sets explained or how to create a set in Python.
Basic syntax
my_set.add(item)
Important points:
- Call
add()on an existing set - Pass exactly one item
- The item must be hashable
A hashable value is a value Python can safely store in a set. Common hashable values include:
- Numbers
- Strings
- Tuples
- Booleans
Simple example
Here is a basic example:
colors = {"red", "blue"}
colors.add("green")
print(colors)
Possible output:
{'red', 'green', 'blue'}
The new value "green" was added to the set.
One important thing to remember: sets are unordered collections. That means you should not rely on the printed order of values.
For example, this is also valid output:
{'blue', 'red', 'green'}
The values are the same. Only the display order is different.
What happens with duplicate values
Sets store unique values only. If you try to add a value that is already in the set, Python does not raise an error.
numbers = {1, 2, 3}
numbers.add(2)
print(numbers)
Output:
{1, 2, 3}
The set stays the same because 2 was already present.
add() vs update()
Use add() when you want to add one item.
letters = {"a", "b"}
letters.add("c")
print(letters)
Use update() when you want to add multiple items from an iterable such as a list, tuple, or set.
letters = {"a", "b"}
letters.update(["c", "d", "e"])
print(letters)
Key difference
add()adds one itemupdate()adds multiple items from an iterable
If you want to learn that method next, see Python set update() method.
Return value
set.add() returns None.
That means you should not assign its result back to the variable.
Correct
my_set = {1, 2, 3}
my_set.add(4)
print(my_set)
Wrong
my_set = {1, 2, 3}
my_set = my_set.add(4)
print(my_set)
Output:
None
This is a common beginner mistake. The method changes the original set directly.
Items you can and cannot add
You can add hashable values such as:
- Numbers
- Strings
- Tuples
- Booleans
Example:
items = set()
items.add(10)
items.add("hello")
items.add((1, 2))
items.add(True)
print(items)
You cannot add mutable values such as:
- Lists
- Dictionaries
- Sets
Example that causes an error:
items = {1, 2, 3}
items.add([4, 5])
Error:
TypeError: unhashable type: 'list'
This happens because lists are mutable, so they cannot be stored as set items.
If you get this error, see how to fix TypeError: unhashable type.
Common mistakes
Here are some common problems beginners run into with set.add():
- Using
add()when you need to insert multiple values - Expecting
add()to return the updated set - Trying to add a list or dictionary to a set
- Confusing
set.add()with listappend() - Expecting sets to keep insertion order for display logic
Helpful checks while debugging:
print(my_set)
print(type(my_set))
print(value)
print(type(value))
help(set.add)
Also remember that set.add() is not the same as adding items to a list. If you are working with lists instead, see how to add an item to a list in Python.
FAQ
Does set.add() return a new set?
No. It changes the existing set and returns None.
What if I add a value that is already in the set?
Nothing breaks. The set stays the same because sets keep only unique values.
How do I add more than one item to a set?
Use set.update() instead of add().
Can I add a list to a set?
No. Lists are mutable and unhashable, so Python raises a TypeError.
See also
Once you understand add(), the next useful step is learning when to use a set, list, or another collection type so you can choose the right method for the task.