🚀 Supercharge your YouTube channel's growth with AI.
Try YTGrowAI FreePython Set Union: How to Combine Sets

Combining two Python sets can produce a result while leaving the first set unchanged. Set union collects distinct members, using union() or | for set operands.
This article explains how to combine several sets or iterables and how to update an existing set when you want it to change.
TL;DR
Call set.union() to return a new set containing distinct elements from the inputs, or use | when every operand is a set.
Use left.union(right, more) when you want to keep left unchanged or accept other iterables.
Use left | right for concise union of sets.
Use update() or |= when you intend to modify the existing set.
What is Python set union?
A union combines membership from two or more collections.
Package names provide one possible member type in either source collection.
Write the method as first.union(other, …), with the receiver first.
How to combine Python sets step by step
Choose the method for mixed iterable inputs or the operator for set operands, then check whether the original data should remain untouched.
Step 1: Combine two sets with union()
Call union() on the first set and pass the second set. This lets later code keep using the original members.
left = {"api", "docs"}
right = {"docs", "tests"}
combined = left.union(right)
print(sorted(combined))
print(sorted(left))
The captured output is:
['api', 'docs', 'tests']
['api', 'docs']
TypeError: unsupported operand type(s) for |: 'set' and 'list'
['api', 'docs', 'tests']
The shared value docs appears once in the result. Sorting makes the displayed order deterministic.
The sorted result is [‘api’, ‘docs’, ‘tests’]. The original prints [‘api’, ‘docs’]. That confirms union() returns the combined members while preserving left.
Step 2: Use the | operator for set operands
When both operands are sets, | combines their membership in a compact form.
combined = left | right
print(sorted(combined))
You can chain the operator for several sets, such as first | second | third.
Step 3: Combine several sets or iterables
A method call can take lists, tuples, or other iterables as separate arguments. A string contributes individual characters, so pass a collection containing the full string if that is the intended member.
base = {"api"}
combined = base.union(["docs", "tests"], ("docs", "examples"))
print(sorted(combined))
To union an iterable holding sets, unpack it into the method call with base.union(*groups). The iterable needs to contain zero or more arguments accepted by union(). The receiving set remains part of the result even when groups is empty.
Step 4: Update the existing set when needed
The demo uses update() when changing the existing set is intended.
members.update(["tests", "docs"])
members |= {"examples"}
Use update() when the existing object should change. This matters when another variable refers to that same set.

Set union edge cases
Operand types and hashability account for the errors shown here. The table pairs each case with its symptom and a working fix.
| Case | What happens | What to do |
|---|---|---|
| Set with list using | | TypeError: unsupported operand type(s) for |: ‘set’ and ‘list’ | Use union(list_values) or convert the list with set(list_values). |
| List supplied to union() | The list elements are added to the result. | Use this when the iterable contents are the intended members. |
| Repeated values | Only one equal value remains in the set. | Use a list or collections.Counter if occurrences matter. |
| Mutable value such as a list inside a set | TypeError because set members must be hashable. | Use immutable members such as tuples when their contents are hashable. |
| Empty set literal written as {} | Python creates an empty dictionary. | Write set() to create an empty set. |
Repeated values collapse to one member, as the table above shows. For case-insensitive text membership, normalize labels before creating or combining sets.
For more set operations, see Python set intersection to find shared members and Python set difference to find members present only on one side.
Conclusion
Choose the operation by input type and whether the receiver should change.
The Python documentation for set and frozenset types lists these operations and their behavior. Related guides cover set intersection and set difference.
FAQ
These answers cover common choices when combining sets.
Does set.union() change the original set?
No. set.union() returns a new set and leaves the receiver unchanged. Use update() or |= to modify the existing set.
Can Python union a set with a list?
Yes, call the set’s union() method with the list. The | operator requires set operands, so convert the list to a set before using it with |.
How do I union more than two sets?
Pass several arguments to first.union(second, third), or chain set operands with first | second | third.


