Home »
Python »
Python programs
Python program to concatenate tuples to nested tuple
In this problem, we are given two tuples consisting of integer values. Our task is to create a Python program to concatenate the tuples to a nested tuple.
Submitted by Shivang Yadav, on January 10, 2022
While working with programs in python, we need to concatenate collections to a single nested one in order to save memory and make an efficient algorithm for extracting the data. Here, we will see a python program to concatenate tuples to nested tuples.
Before going further with the problem, let's recap some basic topics that will help in understanding the solution.
Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.
Tuples in Python is a collection of items similar to list with the difference that it is ordered and immutable.
Example:
tuple = ("python", "includehelp", 43, 54.23)
Concatenating tuples to nested tuple
We are given multiple tuples consisting of integer elements. We need to create a Python program to concatenate the tuples into a single nested tuple.
Input:
(5, 6, 1, 8)
(7, 2, 9, 3)
Output:
((5, 6, 1, 8), (7, 2, 9, 3))
This task can be performed by creating a tuple which is the concatenation of both tuples. This task can be performed in python using multiple different methods.
Method 1:
One method to perform the task is by using the addition operator "+" on the tuples which will add them both creating a new tuple consisting of both the tuple. Generally, the method creates a tuple with no nesting but we will add ',' to each tuple while passing this will return a nested tuple.
# Python program to concatenate
# tuples to nested tuple
# Initializing and printing tuples
myTuple1 = (3, 4)
myTuple2 = (5, 6)
print("The elements of tuple 1 are " + str(myTuple1))
print("The elements of tuple 2 are " + str(myTuple2))
# Concatenating tuples
concTuple = (myTuple1, ) + (myTuple2, )
print("The nested tuple after concatenation : " + str(concTuple))
Output:
The elements of tuple 1 are (3, 4)
The elements of tuple 2 are (5, 6)
The nested tuple after concatenation : ((3, 4), (5, 6))
Method 2:
Another method to concatenate the tuples is by using the sum function. While passing we will be passing the tuples in a different way. This will create the new tuple with tuples as elements.
# Python program to concatenate
# tuples to nested tuple
# Initializing and printing tuples
myTuple1 = (3, 4)
myTuple2 = (5, 6)
print("The elements of tuple 1 are " + str(myTuple1))
print("The elements of tuple 2 are " + str(myTuple2))
# Concatenating tuples
concTuple = sum(((myTuple1, ), (myTuple2, )), ())
print("The nested tuple after concatenation : " + str(concTuple))
Output:
The elements of tuple 1 are (3, 4)
The elements of tuple 2 are (5, 6)
The nested tuple after concatenation : ((3, 4), (5, 6))
Python Tuple Programs »