# Union of Python Tuples

A tuple, in python, is a sequential data structure that consists of a number of values. It is an immutable data structure. Hence, it is tricky to get a union of two tuples. Through this article we will try to dive into a few of the best possible approaches and understand their implementation in Python.

Tuples, in python, are immutable, but they can contain mutable objects. Similarly, we can take help of mutable data structures to get our desired union.

The union of Python tuples finds applications in removing duplicates, merging data sources, set operations, data deduplication, merging multiple results, database operations, handling data overlaps, and set operations in machine learning. It offers versatility and practicality in data manipulation and analysis tasks across different domains.

## Method 1: Using set and ‘+’ operator

We can easily convert a tuple into a set. Set has a unique property that it does not allow repetition of elements, hence union can be achieved by simply adding the both tuples and passing through a set() function.

### Example

#taking two tuples
tuple1 = (1,2,3,4)
tuple2 = (3,4,5,6,7)

#displaying the tuples
print("Tuple 1: "+ str(tuple1))
print("Tuple 2: "+ str(tuple2))

#union set
setUnionTuple = set(tuple1+tuple2)
tupleUnion = tuple(setUnionTuple)

#printing the union of tuples
print("Union of tuples: "+ str(tupleUnion))


### Output

Tuple 1: (1, 2, 3, 4)
Tuple 2: (3, 4, 5, 6, 7)
Union of tuples: (1, 2, 3, 4, 5, 6, 7)


The two tuples are added using the plus operator, which generates a tuple with all the elements contained in both the tuples. It is then converted to a Set, which removes all duplicate elements. Thus, we get a final set which is the resultant union of the two tuples.

## Method 2: Using union method of Set

The union() method of Sets can be cleverly used for our tuple union. We shall convert the individual tuples to sets, and then the union() method gives us the union set. Later the union set can be converted back to a tuple.

### Example

def tupleUnion(tuple1,tuple2):
set1 = set(tuple1)
set2 = set(tuple2)
unionSet = set1.union(set2)
return tuple(unionSet)

tuple1 = (1,2,3,4)
tuple2 = (3,4,5,6,7)

#displaying the tuples
print("Tuple 1: "+ str(tuple1))
print("Tuple 2: "+ str(tuple2))

#union of tuples
tupleUni = tupleUnion(tuple1,tuple2)
print("Union of tuples: "+ str(tupleUni))


### Output

Tuple 1: (1, 2, 3, 4)
Tuple 2: (3, 4, 5, 6, 7)
Union of tuples: (1, 2, 3, 4, 5, 6, 7)


For better implementation and understanding, we have created a function tupleUnion(). The function takes two tuples as input. Now, individual tuples were converted to sets and union() function was used to generate the union of the sets. Lastly, for the desired output the unionset was converted back to tuple.

## Method 3: Set conversion with * operator

In python, we use the * operator to unpack the tuples. We can unpack the tuples and create a set from them, and the set automatically removes the duplicate elements. Hence, we get the desired union in set form and can easily convert it back to tuple. Let’s see the code logic:

### Example

def tupleUnion(tuple1,tuple2):
unionSet = set((*tuple1,*tuple2))
return tuple(unionSet)

#taking two tuples
tuple1 = (1,2,3,4)
tuple2 = (3,4,5,6,7)

#displaying the tuples
print("Tuple 1: "+ str(tuple1))
print("Tuple 2: "+ str(tuple2))

#union of tuples
tupleUni = tupleUnion(tuple1,tuple2)

#printing the union of tuples
print("Union of tuples: "+ str(tupleUni))


### Output

Tuple 1: (1, 2, 3, 4)
Tuple 2: (3, 4, 5, 6, 7)
Union of tuples: (1, 2, 3, 4, 5, 6, 7)


So, you can see in this implementation, the function tupleUnion() accepts two tuples. The tuples with the * operator is the unpacking of the tuples that is used to make our set. The set converted back to tuple and returned is the resultant union.

## Method 4: Utilizing the itertools module

There is an itertools module, that consists of functions creating iterators. In this final method, we will try to utilize the chain() method of that module to produce iterables that can together form a Set. The set can be converted back to tuple to get the desired union.

### Example

import itertools

def tupleUnion(tuple1,tuple2):
chainIterables = itertools.chain(tuple1,tuple2)
unionSet = set(chainIterables)
return tuple(unionSet)

#taking two tuples
tuple1 = (1,2,3,4)
tuple2 = (3,4,5,6,7)

#displaying the tuples
print("Tuple 1: "+ str(tuple1))
print("Tuple 2: "+ str(tuple2))

#union of tuples
tupleUni = tupleUnion(tuple1,tuple2)

#printing the union of tuples
print("Union of tuples: "+ str(tupleUni))


### Output

Tuple 1: (1, 2, 3, 4)
Tuple 2: (3, 4, 5, 6, 7)
Union of tuples: (1, 2, 3, 4, 5, 6, 7)


Firstly, of course we need to import the itertools module. Then in our tuple union function, we will use the chain() function from itertools module to create a chain of iterables. This chain is used to form the union set. Which is converted to tuples as the output.

## Conclusion

Coming to the end, in this article we have explored the possible ways to find the union of a tuple which include using set and ‘+’ operator ; union method of set; set conversion with * operator; Utilizing the itertools module.

Tuples, being immutable, are majorly useful in representing fixed collection of data, and finding their union can be helpful in various data manipulation outcomes.

Updated on: 29-Aug-2023

130 Views 