Python’s set.union(set_1, set_2, ...) creates and returns a new set consisting of the elements that are members of any of the involved sets. The resulting set has at least as many elements as any other set given in the argument list.

Here’s a minimal example that creates a new set arising from the union of two sets s and t:

>>> s = {1, 2, 3, 4}
>>> t = {3, 4, 5}
>>> s.union(t)
{1, 2, 3, 4, 5}

Syntax

Let’s dive into the formal syntax of the set.union() method.

set.union(*sets)

Argument

Data Type

Explanation

*sets

One or more sets

The elements of those sets will be unionized

Return Value of Set union()

The return value of set.union() is a new set consisting of the elements that are members of any set, including the set it is called on. It has at least the number of elements as any other set involved.

Advanced Examples Set Union

There are some subtleties you need to understand regarding the set union method. Let’s dive into them by example!

The straightforward example is to calculate the union of a superset with one of its subsets. In this case, the result is the superset set because all elements in the subset are already elements of the superset, by definition.

>>> {1, 2, 3}.union({1, 2})
{1, 2, 3}

But what if you’d invert this and calculate the union of a subset and a superset? In this case, the result is the same as before:

>>> {1, 2}.union({1, 2, 3})
{1, 2, 3}

Can you compute the union of a set and an empty set? Sure! The return value is the larger set.

>>> {1, 2, 3}.union(set())
{1, 2, 3}

Set Union Multiple Set Arguments

You can compute the union of an original set and an arbitrary number of set arguments. In this case, the return value will be a set that contains elements that are members of any of the involved sets.

All elements are members of the newly created set.

Python Set Union | Operator

A much more concise way to write the set union is the overloaded operator |. When applied to two sets s and t, the result of s | t is the same as calling s.union(t). It computes the union of the sets.

This | notation is more concise and readable. Therefore, you may want to choose the | operator over the set.union() method.

To compute the set union of multiple sets with the | operator, chain together multiple union computations like this: s0 | s1 | s2 | ... | sn.

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

You don’t need to import any library to use the | operator—it is built-in.

Python Set Update vs Union

Both set.update() and set.union() perform the union operation. However, set.update() adds all missing elements to the set on which it is called whereas set.union() creates a new set. Consequently, the return value of set.update() is None (with side effects) and the return value of set.union() is a set (without side effects).

Both sets remain unchanged. However, a new set has been created—and this set is the return value of the operation!

What is the Time Complexity of Set Union in Python?

The runtime complexity of the set.union() method on a set with n elements and a set argument with m elements is O(n + m) because you need to create an empty set and insert all n elements, and then insert all m elements into the newly created set. Inserting an element into a set is O(1), so the runtime complexity is O(n) * O(1) + O(m) * O(1) = O(m+n).

You can see this in the following simple experiment where we run the set method multiple times for increasing set sizes:

I ran this experiment on my Acer Aspire 5 notebook (I know) with Intel Core i7 (8th Gen) processor and 16GB of memory. Here’s the code of the experiment:

import matplotlib.pyplot as plt
import time
sizes = [i * 10**5 for i in range(50)]
runtimes = []
for size in sizes:
s = set(range(size))
t = set(range(0, size, 2))
# Start track time ...
t1 = time.time()
s.union(t)
t2 = time.time()
# ... end track time
runtimes.append(t2-t1)
plt.plot(sizes, runtimes)
plt.ylabel('Runtime (s)')
plt.xlabel('Set Size')
plt.show()

Other Python Set Methods

All set methods are called on a given set. For example, if you created a set s = {1, 2, 3}, you’d call s.clear() to remove all elements of the set. We use the term “this set” to refer to the set on which the method is executed.

Create and return a new set containing all elements of this set except the ones in the given set arguments. The resulting set has at most as many elements as any other.

Replace this set with the symmetric difference, i.e., elements in either this set or the specified set argument, but not elements that are members of both.

Update this set with all elements that are in this set, or in any of the specified set arguments. The resulting set has at least as many elements as any other.