Python Program to Merge two Tuples



In Python, tuples are an immutable sequence type that is commonly used to store a collection of items.

We can define a tuple in python using the round brackets enclosing the data that we wish to store −

Var = (1, ‘a’, 3.7)

There are times when we must use a single variable that has the elements of two or more different tuples. In those cases, we must know the ways we can merge them to create a single variable.

In this article, we will be discussing how to merge two tuples in Python.

Using + operator

As we know that python programming language provides us a various range of operators and functions that make it easy for us to perform complex tasks without having the need of much technical know-how for it.

Using the + operator is an excellent example of the same, as it is used to concatenate not only tuples but strings and lists as well. This is the easiest way you can merge two tuples. The steps involved for the process are simple.

Step 1 - Create two tuples

Step 2 – Create a new tuple and assign it the sum of two tuples as its value.

Example

A = (1, 2, 3)
B = ('a', 'b', 'c')
print("The first tuple, A : ", A)
print("The second tuple, B : ", B)
C = A + B
print("The resulting tuple is, C : ", C)

Output

The first tuple, A : (1, 2, 3)
The second tuple, B : ('a', 'b', 'c')
The resulting tuple is, C : (1, 2, 3, 'a', 'b', 'c')

Using sum()

Another way we can produce a concatenated tuple is by using the sum() method present by default in python. Let us first look into what a sum() function is and how is it used.

The sum function in python is used to take in an iterable like a list, tuple etc. and returns the sum of its elements. But in this case, we will be providing it a tuple of tuples, a nested tuple, whose elements will be the tuples we wish to concatenate, and the sum function will return to us a tuple with all the elements concatenated in it.

Example

A = (1, 2, 3)
B = ('a', 'b', 'c')
print("The first tuple, A : ", A)
print("The second tuple, B : ", B)
C = sum((A, B), ())
print("The resulting tuple is, C : ", C)

Output

The first tuple, A : (1, 2, 3)
The second tuple, B : ('a', 'b', 'c')
The resulting tuple is, C : (1, 2, 3, 'a', 'b', 'c')

Using extend method for lists to concatenate

We will be using type conversion to convert the tuples into lists by using the list method and then we will use the extend method for list objects to concatenate the two lists that we created.

The extend method is used to iterate over an iterable and add items to the end of the first iterable. We will store the result in a variable after converting the resulting list back to a tuple.

Algorithm

Step 1 − Create two tuples with some elements in it

Step 2 − Print the elements of two different tuples

Step 3 − Convert the tuples to a list using the list method

Step 4 − Use the extend method to create a concatenated list

Step 5 − Convert the resulting list to a tuple and then print it

Example

A = (1, 2, 3)
B = ('a', 'b', 'c')
print("The first tuple, A : ", A)
print("The second tuple, B : ", B)
listA = list(A)
listB = list(B)
listA.extend(listB)
C = tuple(listA)
print("The resulting tuple is, C : ", C)

Output

The first tuple, A : (1, 2, 3)
The second tuple, B : ('a', 'b', 'c')
The resulting tuple is, C : (1, 2, 3, 'a', 'b', 'c')

Conclusion

In this article, we focused on three different ways to merge two tuples. We learnt the pythonic way which is simple and easy to use. We also saw how we can make use of default python method, sum(), to achieve similar results. At last, we saw, how can we make use of type conversion and list method, extend(), to concatenate.


Advertisements