In this tutorial, we will look at how to get the union of two or more sets in Python with the help of some examples.

## Union of two Sets

The union operation between two sets results in a set containing the unique elements of both the sets. The following is an example.

**📚 Discover Online Data Science Courses & Programs (Enroll for Free)**

**Introductory ⭐**

- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science

**Intermediate ⭐⭐⭐**

- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization

**Advanced ⭐⭐⭐⭐⭐**

- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science

**🔎 Find Data Science Programs 👨💻 111,889 already enrolled**

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

You can see that the result of union of sets A and B is a set containing all the elements from both these sets (1, 2, 3, 4).

## Set Union in Python

Python comes with a built-in set data structure to implement a set. It also has a number of additional functions to help you with common operations on sets such as union, intersection, difference, etc.

### Union of two sets

To get the union between two sets in Python, you can use the set `union()`

function. The following is the syntax:

**Upskill your career right now →**

# union of two sets - a and b a.union(b)

It returns a new set containing elements resulting from the union of the elements from set a and b. Let’s see an example.

# create two sets a = {1,2,4} b = {3,5,7,1} # union between two sets a.union(b)

Output:

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

You can see that the resulting set contains all the elements from both the sets.

### Union of more than two sets

To get the union of more than two sets at once, you can use the following syntax.

# union of more than two sets, for example, a,b,c,d a.union(b,c,d)

Pass the sets (apart from the first one) as arguments to the `union()`

function. It returns a set containing all the elements from all the sets involved in the union operation. Let’s look at an example.

# create four sets a = {1,2} b = {8,9} c = {0,1,5} d = {4,3} # union between the above four sets a.union(b,c,d)

Output:

{0, 1, 2, 3, 4, 5, 8, 9}

We get the union with elements from all the sets.

**Upskill your career right now →**

Alternatively, you can also use the `|`

operator to get the union between two more sets. For example –

# alternative method e = a | b | c | d print(e)

Output:

{0, 1, 2, 3, 4, 5, 8, 9}

We get the same result as above.

You might also be interested in –

**Subscribe to our newsletter for more informative guides and tutorials. ****We do not spam and you can opt out any time.**