W3docs

Join Sets in Python

Learn every way to join Python sets: union(), update(), the | and |= operators, and in-place methods for intersection, difference, and symmetric difference.

Python provides several ways to combine two or more sets into a single collection. This page covers all of them — the union() method, the update() method, their operator shortcuts (| and |=), joining more than two sets at once, and the related in-place methods for intersection, difference, and symmetric difference.

Quick Comparison

GoalMethodOperatorModifies original?
New set with all elementsunion()|No — returns new set
Add all elements in placeupdate()|=Yes
New set — common elements onlyintersection()&No
Keep only common elementsintersection_update()&=Yes
New set — elements not in otherdifference()-No
Remove elements found in otherdifference_update()-=Yes
New set — elements in one but not bothsymmetric_difference()^No
Keep elements in one but not bothsymmetric_difference_update()^=Yes

Joining Sets with union()

union() returns a new set containing every unique element from all the sets passed in. The original sets are not changed.

Union two sets in Python

python— editable, runs on the server

Output (order may vary — sets are unordered):

{'apple', 'banana', 'cherry', 'mango', 'orange'}

Because set2 already contains "banana", the result only includes it once. That is the defining property of a set: no duplicates.

The | Operator

The pipe | is the operator equivalent of union(). It produces the same result and is often more readable in expressions.

set1 = {"apple", "banana", "cherry"}
set2 = {"orange", "banana", "mango"}

set3 = set1 | set2
print(set3)

Output:

{'apple', 'banana', 'cherry', 'mango', 'orange'}

When to use | vs union(): use | for a quick, readable expression between two sets. Use union() when you need to pass any other iterable (such as a list or tuple) directly — union() accepts any iterable, whereas | requires both operands to be sets.

# union() accepts any iterable
set1 = {1, 2, 3}
result = set1.union([4, 5], (6,))   # list and tuple both work
print(result)

Output:

{1, 2, 3, 4, 5, 6}

Joining Sets with update()

update() adds all elements from one or more other sets (or any iterable) into the existing set. It modifies the original set in place and returns None.

Update a set with another set in Python

python— editable, runs on the server

Output:

{'apple', 'banana', 'cherry', 'mango', 'orange'}

After this call set1 has grown to include all unique elements from set2. set2 itself is unchanged.

The |= Operator

|= is the in-place equivalent of update().

set1 = {"apple", "banana", "cherry"}
set2 = {"orange", "banana", "mango"}

set1 |= set2
print(set1)

Output:

{'apple', 'banana', 'cherry', 'mango', 'orange'}

Joining More Than Two Sets at Once

Both union() and update() accept multiple arguments, so you can combine many sets in a single call.

A = {1, 2, 3}
B = {3, 4, 5}
C = {5, 6, 7}

result = A.union(B, C)
print(result)

Output:

{1, 2, 3, 4, 5, 6, 7}

The same applies to update():

A = {1, 2, 3}
B = {3, 4, 5}
C = {5, 6, 7}

A.update(B, C)
print(A)

Output:

{1, 2, 3, 4, 5, 6, 7}

You can also chain the | operator across several sets:

result = {1, 2} | {3, 4} | {5, 6}
print(result)

Output:

{1, 2, 3, 4, 5, 6}

The same in-place vs. new-set pattern applies to all the other set operations. These are worth knowing alongside update().

intersection_update() and &=

Keeps only the elements that appear in all specified sets.

A = {1, 2, 3, 4}
B = {2, 3, 5}

A.intersection_update(B)
print(A)   # only elements in both A and B

Output:

{2, 3}

difference_update() and -=

Removes every element that also appears in the other set.

A = {1, 2, 3, 4}
B = {2, 4}

A.difference_update(B)
print(A)   # elements in A that are not in B

Output:

{1, 3}

symmetric_difference_update() and ^=

Keeps only the elements that appear in exactly one of the two sets — elements shared by both are discarded.

A = {1, 2, 3}
B = {2, 3, 4}

A.symmetric_difference_update(B)
print(A)   # elements in A or B, but not in both

Output:

{1, 4}

union() vs update() — Which Should You Use?

Use union() (or |) when you want to keep the original sets intact and work with the combined result as a separate value. It is the right choice inside expressions, function return values, and wherever immutability matters.

Use update() (or |=) when you are building up a set incrementally and do not need to preserve the original. It uses slightly less memory because it does not create an extra object.

# Reading pattern: create a single combined set from several sources
all_tags = set()
for article in articles:
    all_tags.update(article["tags"])   # update() is natural here

Practice

Practice
Which of the following statements about joining sets in Python are correct?
Which of the following statements about joining sets in Python are correct?
Was this page helpful?