Python set.union() Method
set.union() combines items from one set with items from one or more other iterables.
It is useful when you want:
- all unique values from multiple collections
- a new set as the result
- to keep the original set unchanged
Quick example
set1 = {1, 2, 3}
set2 = {3, 4, 5}
result = set1.union(set2)
print(result)
# {1, 2, 3, 4, 5}
union() returns a new set containing all unique items from both sets.
What set.union() does
set.union():
- combines items from one set and one or more other iterables
- keeps only unique values
- returns a new set
- does not change the original set
If the same value appears more than once, it will appear only once in the result.
If you are new to sets, see Python sets explained.
Basic syntax
set1.union(set2, set3, ...)
Key points:
- You can pass one or more arguments.
- Each argument can be a set or another iterable.
- The method returns a new set with all unique items.
Example with two sets:
a = {1, 2}
b = {2, 3}
result = a.union(b)
print(result)
What it returns
set.union() always returns a new set.
That returned set contains every unique item found in:
- the original set
- all arguments passed to
union()
Example:
a = {1, 2}
b = {2, 3}
result = a.union(b)
print(result) # {1, 2, 3}
print(a) # {1, 2}
Notice that a did not change.
Also remember:
- sets are unordered
- printed output order is not guaranteed
So you may see the same values in a different order.
Simple example
Here is a basic example with overlapping values:
fruits1 = {"apple", "banana", "orange"}
fruits2 = {"banana", "grape", "orange"}
result = fruits1.union(fruits2)
print(result)
print(fruits1)
Possible output:
{'banana', 'orange', 'apple', 'grape'}
{'banana', 'orange', 'apple'}
What this shows:
"banana"and"orange"appear in both sets- they appear only once in the result
fruits1stays unchanged
Using union() with multiple sets
You can combine three or more sets in one call.
set1 = {1, 2}
set2 = {2, 3}
set3 = {3, 4}
set4 = {4, 5}
result = set1.union(set2, set3, set4)
print(result)
Possible output:
{1, 2, 3, 4, 5}
This is useful when collecting unique values from many sources.
Using union() with other iterables
union() does not only work with sets. It also accepts other iterables such as lists and tuples.
With a list
numbers = {1, 2, 3}
more_numbers = [3, 4, 5, 5]
result = numbers.union(more_numbers)
print(result)
Possible output:
{1, 2, 3, 4, 5}
Even though 5 appears twice in the list, it appears only once in the result.
With a tuple
letters = {"a", "b"}
more_letters = ("b", "c", "d")
result = letters.union(more_letters)
print(result)
Possible output:
{'a', 'b', 'c', 'd'}
With a string
A string is treated as an iterable of individual characters.
chars = {"a", "b"}
result = chars.union("cat")
print(result)
Possible output:
{'a', 'b', 'c', 't'}
This happens because "cat" is processed as:
"c""a""t"
union() vs update()
union() and update() are similar, but they behave differently.
union()
- returns a new set
- does not change the original set
a = {1, 2}
b = {2, 3}
result = a.union(b)
print(result) # {1, 2, 3}
print(a) # {1, 2}
update()
- changes the existing set in place
- does not create a separate result set
a = {1, 2}
b = {2, 3}
a.update(b)
print(a) # {1, 2, 3}
Use union() when you want to keep the original set unchanged.
For the in-place version, see Python set.update() method.
Common beginner mistakes
Expecting union() to change the original set
This is very common:
a = {1, 2}
b = {2, 3}
a.union(b)
print(a)
Output:
{1, 2}
Why? Because union() returned a new set, but you did not save it.
Fix it like this:
a = {1, 2}
b = {2, 3}
a = a.union(b)
print(a)
Assuming the result keeps insertion order
Sets are unordered. Do not rely on the printed order.
a = {3, 1}
b = {2, 4}
print(a.union(b))
The values may not appear in the order you expect.
Passing a string and forgetting it becomes characters
words = {"dog"}
result = words.union("cat")
print(result)
This adds "c", "a", and "t" as separate items, not "cat" as one item.
If you want the whole string as one item, put it inside another iterable:
words = {"dog"}
result = words.union(["cat"])
print(result)
Trying to use unhashable values like lists inside a set
This causes an error:
my_set = {1, 2, [3, 4]}
Lists cannot be set items because they are mutable and unhashable.
But a list can be used as an argument to union():
my_set = {1, 2}
result = my_set.union([3, 4])
print(result)
Common causes of confusion
Beginners often run into problems because they:
- call
union()without saving the returned set - confuse
union()withupdate() - expect duplicates to remain in the result
- use a list as a set item instead of as an iterable argument
If you need a different set operation, compare this method with Python set.intersection() method and Python set.difference() method.
Useful checks while debugging:
print(my_set)
print(type(my_set))
print(result)
help(set.union)
FAQ
Does set.union() change the original set?
No. It returns a new set and leaves the original set unchanged.
Can union() combine more than two sets?
Yes. You can pass multiple sets or iterables in one call.
Can I use union() with a list or tuple?
Yes. union() accepts other iterables, not just sets.
Why is the output order different from my input?
Sets are unordered, so their printed order is not guaranteed.
What is the difference between union() and |?
They both combine sets. The | operator is shorter, but union() can be clearer for beginners.