Python Set union()


Python Set union() method

Python Set union() method finds and returns the union of the two sets.

In this tutorial, you will learn the syntax and usage of Set union() method, with examples.

Syntax of Set union()

The syntax to call union() method is

set.union(s)

Parameters

Set union() method takes one parameter.

ParameterDescription
sRequired
An iterable, like a set, a list, a string, a tuple, etc.
Python Set union() method parameters table

Return value

union() method returns a new set object created from the union of the items in original set and the items in the given iterable.

Usage

Union of two sets

The following statement shows how the union of two sets set_1 and set_2 is done using set.union() method, and store the result in union_output.

# set_1 ∪ set_2
union_output = set_1.union(set_2)

Union of more than two sets

We can also chain the union() method, to find the union of more than two sets in a single statement, as shown in the following code.

# set_1 ∪ set_2 ∪ set_3
union_output = set_1.union(set_2).union(set_3)

Union of a set and a list

The following statement shows how to find the union of a set set_1 and a list list_1 using set.union() method, and store the result in union_output.

# set_1 ∪ list_1
union_output = set_1.union(list_1)

Examples

1. Finding union of two sets in Python

In this example, we shall take two sets in set_1 and set_2. We have to find the union of these two sets.

Call union() method on set_1 object, and pass set_2 object as argument. Store the returned value in union_output.

union_output contains the union of elements from the two sets: set_1 and set_2.

Python Program

# Take two sets
set_1 = {'a', 'b', 'c', 'd'}
set_2 = {'a', 'b', 'd', 'm'}

# Find the union of the sets
union_output = set_1.union(set_2)

print(union_output)

Output

{'a', 'c', 'd', 'm', 'b'}

2. Finding union of a set and a list in Python

In this example, we shall take a set in set_1 and a list in list_1. We have to find the union of these two collections.

Call union() method on set_1 object, and pass list_1 object as argument. Store the returned value in union_output.

union_output contains the union of elements from the set set_1 and the list list_1.

Python Program

# Take a set and a list
set_1 = {'a', 'b', 'c', 'd'}
list_1 = ['a', 'm', 'k']

# Find the union of set and list
union_output = set_1.union(list_1)

print(union_output)

Output

{'m', 'k', 'd', 'c', 'a', 'b'}

3. Finding union of three sets in Python

Set union() method supports chaining, and therefore, you can find the union of more than two sets in a single line.

In this example, we take three sets and find the union of these three sets by chaining union() method.

Python Program

# Take two sets
set_1 = {'a', 'b', 'c', 'd'}
set_2 = {'a', 'b', 'd', 'm'}
set_3 = {'d', 'p', 's', 'v'}

# Find the union of the sets
union_output = set_1.union(set_2).union(set_3)

print(union_output)

Output

{'v', 's', 'c', 'a', 'm', 'd', 'p', 'b'}

Summary

In this Python Set Tutorial, we learned how to use Set union() method to find the union of items in the given sets in Python, with the help of well detailed examples.