HOWTO · Python
How to Join Two Sets in Python
This article introduces how to join two sets in Python.
On this page
Python sets store unique elements, so joining two sets means computing their union: a collection containing every element from either set with duplicates collapsed into one. You can do this with the | operator, the union() method, or the in-place forms update() and |=, depending on whether you want a new set or want to change an existing one.
Combine sets with the union operator (|)
The union operator | takes two sets and returns a new set holding every element that appears in either of them. The original sets are not modified.
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
result_set = set1 | set2
print(result_set)
print(set1)
print(set2)
Output:
{1, 2, 3, 4, 5, 6}
{1, 2, 3, 4}
{3, 4, 5, 6}
The shared values 3 and 4 appear only once in result_set, and set1 and set2 keep their original elements. Both operands of | must be set-like (set or frozenset); combining a set with a list using | gives a TypeError, so convert the list with set() first.
Add elements in place with update() or |=
When you want an existing set to absorb another collection’s elements rather than building a new set, use the update() method or the |= augmented assignment. Both modify the set on the left-hand side instead of returning a new one.
The two differ in what they accept. update() takes any iterable — a set, list, tuple, or string — while |= requires a set-like right operand, matching the restrictions of |.
numbers = {1, 2, 3}
numbers.update([3, 4, 5])
print(numbers)
numbers |= {6, 7}
print(numbers)
Output:
{1, 2, 3, 4, 5}
{1, 2, 3, 4, 5, 6, 7}
The update() call adds the list’s new values directly to numbers, and |= then merges the second set into the same object. Use these forms only when mutation is intended, because every other reference to that set sees the change too.
Use union() to combine multiple sets or iterables
The union() method is the method counterpart of |. It returns a new set and leaves its inputs untouched, but unlike the operator it accepts any number of arguments, and each argument can be any iterable rather than only a set.
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
result_set = set1.union(set2, {7, 8}, [9, 10])
print(result_set)
print(set1)
Output:
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
{1, 2, 3, 4}
Here union() merges a set, a set literal, and a list in a single call, while set1 itself keeps its original elements. When every operand is already a set, a.union(b) produces the same result as a | b.
Choose the operation that matches whether you need a new set
Pick the form based on the result you want:
- You need a new set and want the originals preserved: use
|when both operands are sets, orunion()when an operand is a list or other iterable or when combining three or more collections. - You want to change an existing set: use
update()for any iterable, or|=for set operands. - You are reading existing code:
a.union(b)anda | bmean the same thing for sets, anda.update(b)anda |= bare their in-place counterparts.
For most code, result = set1 | set2 is the direct answer to joining two sets; reach for union() when operands may not be sets, and for update() or |= only when in-place mutation is the goal.