Python Set update() Method
set.update() adds multiple items from another iterable into an existing set.
Use it when you want to change a set in place with values from a list, tuple, set, string, or another iterable. It is different from set.add(), which adds only one item.
Quick answer
numbers = {1, 2}
numbers.update([2, 3, 4])
print(numbers)
# {1, 2, 3, 4}
Use update() to add multiple items from a list, tuple, set, or other iterable into an existing set.
What set.update() does
set.update():
- Adds items from another iterable into the current set
- Changes the original set in place
- Ignores duplicate values automatically
- Does not return a new set
This means the set itself is modified.
colors = {"red", "blue"}
colors.update(["blue", "green"])
print(colors)
# {'red', 'blue', 'green'}
Even though "blue" was added again, it appears only once because sets store unique values.
Basic syntax
The basic syntax is:
my_set.update(iterable)
You can pass:
- A list
- A tuple
- Another set
- A string
- Any other iterable
You can also pass multiple iterables at once:
my_set.update(a, b, c)
Example:
values = {1}
values.update([2, 3], (4, 5), {6})
print(values)
# {1, 2, 3, 4, 5, 6}
If you are new to sets, see how to create a set in Python.
What it returns
set.update() returns None.
This is a very common beginner mistake:
numbers = {1, 2}
result = numbers.update([3, 4])
print(result)
# None
The updated values are stored in the original set, not in result.
Correct usage:
numbers = {1, 2}
numbers.update([3, 4])
print(numbers)
# {1, 2, 3, 4}
Example: update with a list
A list is one of the most common things to pass to update().
numbers = {1, 2}
numbers.update([2, 3, 4, 4])
print(numbers)
# {1, 2, 3, 4}
What happens here:
2is already in the set, so nothing changes for that value3and4are added- The second
4is ignored because sets do not keep duplicates
This is one reason sets are useful when you want unique values. For example, you can remove duplicates from a list in Python by converting the list to a set.
Example: update with another set
You can use update() to combine values from one set into another.
set_a = {1, 2, 3}
set_b = {3, 4, 5}
set_a.update(set_b)
print(set_a)
# {1, 2, 3, 4, 5}
This is useful when you want to keep only unique values and modify the original set.
If you want a new combined set instead of changing the original, use set.union().
set_a = {1, 2, 3}
set_b = {3, 4, 5}
combined = set_a.union(set_b)
print(combined)
# {1, 2, 3, 4, 5}
print(set_a)
# {1, 2, 3}
Example: update with a string
A string is iterable, so update() adds each character separately.
letters = {"x"}
letters.update("cat")
print(letters)
# {'x', 'c', 'a', 't'}
This often surprises beginners.
update("cat") does not add "cat" as one item. It adds:
"c""a""t"
If you want to add one whole value, use set.add():
words = {"dog"}
words.add("cat")
print(words)
# {'dog', 'cat'}
update() vs add()
The difference is simple:
add()adds one itemupdate()adds multiple items from an iterable
Example with add():
fruits = {"apple"}
fruits.add("banana")
print(fruits)
# {'apple', 'banana'}
Example with update():
fruits = {"apple"}
fruits.update(["banana", "orange"])
print(fruits)
# {'apple', 'banana', 'orange'}
Important warning with strings:
fruits = {"apple"}
fruits.update("kiwi")
print(fruits)
# Adds 'k', 'i', 'w', 'i' as characters
If you wanted "kiwi" as one item, use add() instead.
update() vs union()
Both can combine values, but they work differently.
update()
- Changes the existing set
- Returns
None
a = {1, 2}
b = {2, 3}
a.update(b)
print(a)
# {1, 2, 3}
union()
- Returns a new set
- Does not change the original set
a = {1, 2}
b = {2, 3}
c = a.union(b)
print(c)
# {1, 2, 3}
print(a)
# {1, 2}
Choose update() when you want to modify the original set in place. Choose union() when you want a new set.
Common errors and confusion
Here are the most common problems beginners run into with set.update().
1. Saving the return value
This is wrong if you expect a set back:
my_set = {1, 2}
result = my_set.update([3, 4])
print(result)
# None
Use the original set after calling update():
my_set = {1, 2}
my_set.update([3, 4])
print(my_set)
# {1, 2, 3, 4}
2. Passing a non-iterable
This causes an error:
my_set = {1, 2}
my_set.update(5)
You will get a TypeError because 5 is an integer, not an iterable.
Use an iterable instead:
my_set = {1, 2}
my_set.update([5])
print(my_set)
# {1, 2, 5}
If you see this problem, read how to fix TypeError: 'int' object is not iterable.
3. Expecting duplicates to stay
Sets only keep unique values.
my_set = {1, 2}
my_set.update([2, 2, 2, 3])
print(my_set)
# {1, 2, 3}
4. Using update() when add() is better
If you want to add one single value, especially a string, add() is usually the better choice.
my_set = {"dog"}
my_set.add("cat")
print(my_set)
# {'dog', 'cat'}
FAQ
Does set.update() return a new set?
No. It changes the existing set and returns None.
Can I use update() with a list?
Yes. Any iterable like a list, tuple, set, or string can be used.
What is the difference between add() and update()?
add() adds one item. update() adds multiple items from an iterable.
Why did my string get split into letters?
Because strings are iterable, so update() adds each character separately.
How do I combine two sets without changing the original?
Use union() instead of update().
See also
Python set add() methodPython set union() methodHow to create a set in PythonHow to remove duplicates from a list in PythonTypeError: 'int' object is not iterablefix
Next, learn when to use set.add() for a single value and set.union() when you want a new combined set without changing the original.