Python | How to Concatenate tuples to nested tuples
Last Updated :
12 Apr, 2023
Sometimes, while working with tuples, we can have a problem in which we need to convert individual records into a nested collection yet remaining as separate element. Usual addition of tuples, generally adds the contents and hence flattens the resultant container, this is usually undesired. Let’s discuss certain ways in which this problem is solved.
Method #1 : Using + operator + “, ” operator during initialization In this method, we perform the usual addition of tuple elements, but while initializing tuples, we add a comma after the tuple so that they don’t get flattened while addition.
- Two tuples are initialized using the comma notation and assigned to variables test_tup1 and test_tup2 respectively.
- The original tuples are printed using the print function and string concatenation with the help of the str function to convert the tuple to a string.
- The tuples are concatenated using the + operator and assigned to a new variable called res.
- The result is printed using the print function and string concatenation with the help of the str function to convert the tuple to a string.
Python3
test_tup1 = ( 3 , 4 ),
test_tup2 = ( 5 , 6 ),
print ( "The original tuple 1 : " + str (test_tup1))
print ( "The original tuple 2 : " + str (test_tup2))
res = test_tup1 + test_tup2
print ( "Tuples after Concatenating : " + str (res))
|
Output :
The original tuple 1 : ((3, 4), )
The original tuple 2 : ((5, 6), )
Tuples after Concatenating : ((3, 4), (5, 6))
Time complexity: O(1)
Auxiliary space: O(1)
Method #2 : Using “, ” operator during concatenation
This task can be performed by applying “, ” operator during concatenation as well. It can perform the safe concatenation.
- First, two tuples test_tup1 and test_tup2 are initialized with values (3, 4) and (5, 6) respectively.
- The original values of both tuples are printed using the print() function with the help of string concatenation using the + operator.
- The tuples are concatenated to create a nested tuple using the , operator and assigned to the variable res. Here, the + operator is used to concatenate two tuples and create a new tuple.
- Finally, the result is printed using the print() function and string concatenation using the + operator. The variable res contains the nested tuple created by concatenating test_tup1 and test_tup2.
Python3
test_tup1 = ( 3 , 4 )
test_tup2 = ( 5 , 6 )
print ( "The original tuple 1 : " + str (test_tup1))
print ( "The original tuple 2 : " + str (test_tup2))
res = ((test_tup1, ) + (test_tup2, ))
print ( "Tuples after Concatenating : " + str (res))
|
Output :
The original tuple 1 : ((3, 4), )
The original tuple 2 : ((5, 6), )
Tuples after Concatenating : ((3, 4), (5, 6))
Time complexity: O(1) (constant time)
Auxiliary space: O(1) (constant space)
Method #3 : Using list(),extend() and tuple() methods
Python3
test_tup1 = ( 3 , 4 ),
test_tup2 = ( 5 , 6 ),
print ( "The original tuple 1 : " + str (test_tup1))
print ( "The original tuple 2 : " + str (test_tup2))
test_tup1 = list (test_tup1)
test_tup2 = list (test_tup2)
test_tup1.extend(test_tup2)
print ( "Tuples after Concatenating : " + str ( tuple (test_tup1)))
|
Output
The original tuple 1 : ((3, 4),)
The original tuple 2 : ((5, 6),)
Tuples after Concatenating : ((3, 4), (5, 6))
Time complexity: O(1), which means it’s a constant time operation.
Auxiliary space: O(n), where n is the size of the concatenated tuple.
Method #4: Using the itertools.chain() function
The itertools.chain() function takes multiple iterables and returns a single iterator that yields all the elements from each of the iterables in sequence. By passing the two tuples as arguments to itertools.chain(), we can concatenate them into a single tuple, which can then be converted to a nested tuple using the tuple() function.
Python3
import itertools
test_tup1 = ( 3 , 4 ),
test_tup2 = ( 5 , 6 ),
res = tuple (itertools.chain(test_tup1, test_tup2))
print ( "Tuples after Concatenating : " , res)
|
Output
Tuples after Concatenating : ((3, 4), (5, 6))
Time complexity: O(n) where n is the total number of elements in both tuples.
Auxiliary space: O(n), where n is the total number of elements in both tuples.
Method #5: Using the reduce() method of functools
This program demonstrates how to concatenate two tuples into a nested tuple using the reduce() method from the functools module. It initializes two tuples, concatenates them using reduce(), and prints the result.
Python3
import functools
test_tup1 = ( 3 , 4 ),
test_tup2 = ( 5 , 6 ),
print ( "The original tuple 1 : " + str (test_tup1))
print ( "The original tuple 2 : " + str (test_tup2))
res = functools. reduce ( lambda x, y: x + y, (test_tup1, test_tup2))
print ( "Tuples after Concatenating : " + str (res))
|
Output
The original tuple 1 : ((3, 4),)
The original tuple 2 : ((5, 6),)
Tuples after Concatenating : ((3, 4), (5, 6))
Time complexity: O(n), where n is the total number of elements in the input tuples.
Auxiliary space: O(n), where n is the total number of elements in the input tuples.
Method 6 : using the extend() method of the list class.
Approach:
- Initialize tuples test_tup1 and test_tup2 with values (3, 4) and (5, 6) respectively.
- Create an empty list, last.
- Use the extend() method to add tuples test_tup1 and test_tup2 to last.
- Use the tuple() constructor to convert lst into a nested tuple.
- Assign the nested tuple to the variable res.
- Print the concatenated nested tuple.
Python3
test_tup1 = ( 3 , 4 )
test_tup2 = ( 5 , 6 )
lst = []
lst.extend(test_tup1)
lst.extend(test_tup2)
res = tuple ([lst[i:i + 2 ] for i in range ( 0 , len (lst), 2 )])
print ( "Tuples after Concatenating : " , res)
|
Output
Tuples after Concatenating : ([3, 4], [5, 6])
Time complexity of this method is O(n) where n is the number of elements in both tuples.
Auxiliary space required is O(n) as we are creating a list to store the concatenated tuples.
Please Login to comment...