Python Set union()

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)
ArgumentData TypeExplanation
*setsOne or more setsThe 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.

Here’s an example:

>>> {1, 2, 3, 4, 5, 6}.union({0, 2}, {42, 3, 4}, {33, 3, 5})
{0, 1, 2, 3, 4, 5, 6, 33, 42}

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.

Here’s a basic example:

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

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).

Here’s an example of the set.update() method:

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

The original set s is modified and now contains five elements after the update. There is no return value, so you need to separately print out the set.

Here’s an example of the set.union() method:

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

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:

What is the Time Complexity of Set Union in Python?
Figure: The runtime complexity increases approximately linearly with the number of set elements.

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.

add()Add an element to this set
clear()Remove all elements from this set
copy()Create and return a flat copy of this set
difference()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.
difference_update()Remove all elements from this set that are members of any of the given set arguments.
discard()Remove an element from this set if it is a member, otherwise do nothing.
intersection()Create and return a new set that contains all elements that are members of all sets: this and the specified set(s).
intersection_update()Removes all elements from this set that are not members in all other specified sets.
isdisjoint()Return True if no element from this set is a member of any other specified set. Sets are disjoint if and only if their intersection is the empty set.
issubset()Return True if all elements of this set are members of the specified set argument.
issuperset()Return True if all elements of the specified set argument are members of this set.
pop()Remove and return a random element from this set. If the set is empty, it’ll raise a KeyError.
remove()Remove and return a specific element from this set as defined in the argument. If the set doesn’t contain the element, it’ll raise a KeyError.
symmetric_difference()Return a new set with elements in either this set or the specified set argument, but not elements that are members of both.
symmetric_difference_update()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.
union()Create and return a new set with all elements that are in this set, or in any of the specified set arguments.
update()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.