# Union of set of Tuples in Python

In this tutorial, we will see how to find the union of sets of tuples in python. Let us start with the definition of the union in set theory.

The **union** of sets is the set of every element in the collection of sets. In the case of duplicate elements in various sets, the resultant union will contain the particular element only once. The symbol for the union is ‘**U**‘.

This problem is based on finding the union of sets of tuples which means the set consists of elements that are tuples. Let us see an example of the union of sets of tuples.,

## OR OPERATOR (|) in Python

We can use the OR operator (|) in python to obtain the union of a set of tuples. Use the OR operator directly in between the variables whose union needs to be obtained.

Let us see a program based on this concept.

#Variable Definition. A = {('a', 1), ('b', 2)} B = {('b', 2), ('c', 3)} C = {('a', 1),('c', 3),('d', 4)} # B union C. D = B | C #A union B. print("A U B = ",A | B) #B union C. print("B U C = ", D) #A union B union C. print("A U B U C = ", A | B | C)

##### Output

('A U B = ', set([('a', 1), ('b', 2), ('c', 3)])) ('B U C = ', set([('a', 1), ('b', 2), ('c', 3), ('d', 4)])) ('A U B U C = ', set([('a', 1), ('b', 2), ('c', 3), ('d', 4)]))

## UNION() METHOD in Python

The set union() method will return the union of set variables that are passed as arguments. The first set calls the union() method using the dot operator (.), while the other set variables are passed as arguments.

#### Syntax

setVariables.union(setVariables...)

Let us see a python program based on the union() method concept.

#Variable Definition. A = {('a', 1), ('b', 2)} B = {('b', 2), ('c', 3)} C = {('a', 1),('c', 3),('d', 4)} # B union C. D = B.union(C) print("Using union() method") #A union B. print("A U B = ", A.union(B)) #B union C. print("B U C = ", D) #A union B union C. print("A U B U C = ", A.union(B, C))

##### Output

Using union() method ('A U B = ', set([('a', 1), ('b', 2), ('c', 3)])) ('B U C = ', set([('a', 1), ('b', 2), ('c', 3), ('d', 4)])) ('A U B U C = ', set([('a', 1), ('b', 2), ('c', 3), ('d', 4)]))

We can notice that the output of both the example programs is essentially the same. Hence we can conclude that the usage of OR operator and union() method yields the same results.

Learners can also read,

## Leave a Reply

You must be logged in to post a comment.