In this tutorial, we will learn how to calculate the union of sets of tuples in Python. Let us begin by defining the union in set theory.

The set of every element in the collection of sets is the set of the union of sets. In the case of duplicate elements in different sets, the final union will only contain the specific element once. The letter â€˜U’ represents the union.

This problem is centered on finding the union of sets of tuples, which indicates that the set is made up of tuple elements.

**Examples:**

**Example1:**

**Input:**

first = {('hello', 5), ('this', 100)} second = {('this', 100), ('is', 200)} third = {('hello', 5), ('btechgeeks', 461), ('python', 234)}

**Output:**

first Union Second = {('is', 200), ('this', 100), ('hello', 5)} Second Union third = {('python', 234), ('is', 200), ('this', 100), ('hello', 5), ('btechgeeks', 461)} first Union Second Union third = {('python', 234), ('is', 200), ('this', 100), ('hello', 5), ('btechgeeks', 461)}

## Program to Union of Set of Tuples in Python

Below are the ways to find the union of the set of tuples in Python.

Are you new to the java programming language? We recommend you to ace up your practice session with these Basic Java Programs Examples

### Method #1: UsingÂ or( | ) operator

**Approach:**

- Give the set of tuples as static input and store them in Separate Variables.
- In Python, we may retrieve the union of a set of tuples by using the OR operator (|). In order to acquire the union of two variables, use the OR operator directly between them.
- Calculate the union using | operator and store it in a variable.
- Print the union.
- The Exit of the Program.

**Below is the implementation:**

# Give the set of tuples as static input and store them in Separate Variables. first = {('hello', 5), ('this', 100)} second = {('this', 100), ('is', 200)} third = {('hello', 5), ('btechgeeks', 461), ('python', 234)} # In Python, we may retrieve the union of a set of tuples # by using the OR operator (|). In order to acquire the union of two variables, # use the OR operator directly between them. # Calculate the union using | operator and store it in a variable. reslt1 = second | third reslt2 = first | second reslt3 = first | second | third # Print the union of first and second print("first Union Second = ", reslt2) # print the union of second and third print("Second Union third = ", reslt1) # print the union of first second and third. print("first Union Second Union third = ", reslt3)

**Output:**

first Union Second = {('this', 100), ('is', 200), ('hello', 5)} Second Union third = {('this', 100), ('hello', 5), ('btechgeeks', 461), ('python', 234), ('is', 200)} first Union Second Union third = {('this', 100), ('hello', 5), ('btechgeeks', 461), ('python', 234), ('is', 200)}

### Method #2: Using union() method

**Approach:**

- Give the set of tuples as static input and store them in Separate Variables.
- The set union() method returns the union of the set variables supplied as parameters. The first set uses the dot operator (.) to call the union() method, and the other set variables are given as arguments.
- Calculate the union using the union() method and store it in a variable.
- Print the union.
- The Exit of the Program.

**Below is the implementation:**

# Give the set of tuples as static input and store them in Separate Variables. first = {('hello', 5), ('this', 100)} second = {('this', 100), ('is', 200)} third = {('hello', 5), ('btechgeeks', 461), ('python', 234)} '''The set union() method returns the union of the set variables supplied as parameters. The first set uses the dot operator (.) to call the union() method, and the other set variables are given as arguments. Calculate the union using the union() method and store it in a variable.''' reslt1 = second.union(third) reslt2 = first.union(second) reslt3 = first.union(second, third) # Print the union of first and second print("first Union Second = ", reslt2) # print the union of second and third print("Second Union third = ", reslt1) # print the union of first second and third. print("first Union Second Union third = ", reslt3)

**Output:**

first Union Second = {('is', 200), ('this', 100), ('hello', 5)} Second Union third = {('python', 234), ('is', 200), ('this', 100), ('hello', 5), ('btechgeeks', 461)} first Union Second Union third = {('python', 234), ('is', 200), ('this', 100), ('hello', 5), ('btechgeeks', 461)}

**Related Programs**:

- program to clear the rightmost set bit of a number in cpp and python
- python program to sort a list of tuples in increasing order by the last element in each tuple
- python program to create a list of tuples with the first element as the number and second element as the square of the number
- python program to group words with the same set of characters
- 7 ways to add all elements of list to set in python
- python how to sort a list of tuples by second item
- python read csv into a list of lists or tuples or dictionaries import csv to list