Python set.union() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
set method

Definition

union(*others) builds a new set that holds every item found in the set or in any of the iterables you pass. Duplicates appear only once, and the original set is not changed.

The arguments can be any iterable, so a list, a tuple, or even a string works. With no arguments, union() simply returns a copy of the set.

set.union() is an instance method of the built-in set type: call it on a set with dot notation, such as club.union(...). No import is needed.

Syntax

Pass zero or more iterables, and keep the returned set in a variable.

python
result = s.union(iterable)
result = s.union(iterable1, iterable2)
result = s | other_set
CallWhat it does
{1, 2}.union({2, 3})Returns {1, 2, 3}, a new set. Neither original set changes.
{1, 2}.union([3, 4], (5,))Returns {1, 2, 3, 4, 5}: any iterables work, and several can be passed.
{1, 2} | [3]Raises TypeError: the | operator needs a set on both sides.

a | b is a shortcut for a.union(b), but only between two sets. To add items into the existing set instead of making a new one, use update().

Method signatures
python
set.union(*others)
# returns: a new set with the items of the set and of every iterable
# raises: TypeError if an argument is not iterable or an item is unhashable

Parameters

ParameterDescription
*othersZero or more iterables: set, list, tuple, string, range, dict (its keys), and so on. Every item must be hashable.

Return Value

Return ValueDescription
setA new set with all the items. With no arguments it is a copy of the original, so changing it does not affect the original.

Python Examples

Example 1 — Combine Two Club Memberships

Find everyone who belongs to the chess club or the music club, and check that the originals keep their size.

python
chess = {"Ana", "Ben", "Cho"}
music = {"Cho", "Dev", "Eli"}

everyone = chess.union(music)
print(sorted(everyone))
print(len(chess), len(music), len(everyone))
print(sorted(chess | music) == sorted(everyone))

How It Works

  1. Everyone appears once. Cho is in both clubs but is listed a single time, so five people remain.
  2. The originals keep their size. Both clubs still have three members, because union() builds a separate set.
  3. The operator is the same. chess | music gives the same people, so the sorted lists are equal.

Example 2 — Merge Lists, Tuples, and Strings Into One Set

Pass several kinds of iterables to one call, and see what a string contributes.

python
base = {"red"}
more = base.union(["blue", "red"], ("green",), {"gold"})
print(sorted(more))
print(sorted(base))

letters = {"a"}.union("hello")
print(sorted(letters))

How It Works

  1. Three kinds of arguments. A list, a tuple, and a set are merged in one call, and the repeated "red" is stored once.
  2. The base set is untouched. base still holds only "red", because the result was a new set.
  3. A string adds characters. "hello" contributes h, e, l, and o; the two l letters collapse into one.

Example 3 — Notice That union() Does Not Change the Original

Call union() without keeping the result, then keep it, then compare with update().

python
stock = {"pens", "ink"}
stock.union({"paper"})
print(sorted(stock))

stock = stock.union({"paper"})
print(sorted(stock))

stock.update({"glue"})
print(sorted(stock))

How It Works

  1. A lost result. The first call builds a set with "paper" and throws it away, so stock still has two items.
  2. Assign to keep it. stock = stock.union(...) rebinds the name to the new set, which now includes "paper".
  3. update() changes the set itself. No assignment is needed, and "glue" appears in the same set.

Frequently Asked Questions

A new set containing every item from the set and from all the iterables passed in. The original set is not changed.
union() returns a new set and leaves the original alone. update() adds the items into the original set and returns None.
They give the same result, but the | operator needs sets on both sides. The union() method also accepts lists, tuples, strings, and several arguments.
Yes. A set stores each value once, so an item found in several of the inputs appears only once in the result.
Yes. Pass them all to one call, as in a.union(b, c, d), or chain operators, as in a | b | c.

Next: set.update()

Add every item of one or more iterables to the set, in place.

update() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful