Python Set Union Operations

Introduction

The union of two or more sets is an operation that combines all unique elements from the input sets into a single set. In Python, set union can be accomplished with the union() instance method, the union() class method, and the union operator, |, i.e. the pipe character. Each of these options provides a slightly different capability and convenience.

Using the union() Instance Method

The union() instance method of a set object combines all its elements with items in one or more iterables, given as arguments. It returns a new set containing unique elements from all.

Example:

my_set = {1, 2, 3}
set2 = {3, 4, 5}
lst1 = [5, 6, 7]
union_set = my_set.union(lst1, lst2)
print(union_set)

Output:

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

In this example, the set my_set is combined with the another set, set2, and a list, lst1, using the union() instance method, resulting in a new set, union_set, that contains unique elements from all.

Using the Python Set Union Operator |

The set union operator, |, can be used to perform a union between two or more sets. Like the union() method, it also creates a new set. However, it can only be used with proper set objects and not any other iterable type. Using | with iterable objects that are not sets results in an error.

Example:

set1 = {1, 2, 3}
set2 = {3, 4, 5}
set3 = {6}
union_set = set1 | set2 | set3
print(union_set)

Output:

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

In this example, three sets, set1, set2, and set3, are combined into union set using | operators between them.

Using the union() Class Method

You can use the union() class method to combine the elements of a set and multiple iterable objects into a new single set.

This method is a class method of the set class, and you call it with a set. prefix.

The difference between the union() instance method and union() class method is that the instance method is called on a specific set, whereas the class method includes the set as its first parameter. Following the first argument, which must be a set, the rest of the arguments can be objects of any iterable type.

Example:

set1 = {1, 2, 3}
set2 = {3, 4, 5}
lst = [5, 6, 7]
union_set = set.union(set1, set2, lst)
print(union_set)

Output:

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

The above example combines the sets, set1. an dset2, and the list, lst, into a new set, union_set, which is their union.

Set Union with Mutable Sets

It’s important to note that set union operations do not modify the original sets. Instead, they create a new set containing the union.

If you want to modify one of the original set variables to contain the union result, you should do so explicitly by assigning the result back to it.

Example:

my_set = {1, 2, 3}
another_set = {3, 4, 5}
my_set = my_set.union(another_set)  # Now my_set contains the union
print(my_set)

Output:

{1, 2, 3, 4, 5}

After running the example code, my_set, which was originally {1, 2, 3}, contains the resulting union, {1, 2, 3, 4, 5}.

Summary & Reference for Python Set Union

Set union is a fundamental set operation in Python for combining elements from sets and other iterable objects into a single set.


The union() instance method merges the elements of a set object with items in an iterable to create a new set.

my_set = {1, 2, 3}
more_numbers = [3, 4, 5]
union_set = my_set.union(more_numbers)  # union_set --> {1, 2, 3, 4, 5}

The set union operator, |, allows you to combine two or more proper set objects. It creates a new set containing all unique elements.

set1 = {1, 2, 3}
set2 = {3, 4, 5}
set3 = {6}
union_set = set1 | set2 | set3  # union_set --> {1, 2, 3, 4, 5, 6}

The union() class method of the set class lets you merge a set object, given as the first argument, and multiple iterables, given as subsequent arguments.

set1 = {1, 2, 3}
set2 = {3, 4, 5}
lst = [5, 6, 7]
union_set = set.union(set1, set2, lst)  # union_set --> {1, 2, 3, 4, 5, 6, 7}

Set union operations don’t modify the original sets but create new ones. If you want to modify one of the original set variables with the union result, you should assign the result back explicitly.

my_set = {1, 2, 3}
another_set = {3, 4, 5}
my_set = my_set.union(another_set) #  my_set --> {1, 2, 3, 4, 5}