Python | Ways to concatenate tuples
Last Updated :
18 May, 2023
Many times, while working with records, we can have a problem in which we need to add two records and store them together. This requires concatenation. As tuples are immutable, this task becomes little complex. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using + operator This is the most Pythonic and recommended method to perform this particular task. In this, we add two tuples and return the concatenated tuple. No previous tuple is changed in this process.
step-by-step approach :
- Initialize two tuples with elements (1, 3, 5) and (4, 6) respectively and assign them to the variables test_tup1 and test_tup2.
- Print the original values of the two tuples using print() function and concatenate them with the string using + operator.
- Concatenate the two tuples test_tup1 and test_tup2 using + operator and assign the result to a new variable res.
- Print the concatenated tuple res using print() function and concatenate it with the string using + operator.
- End of the program.
Python3
test_tup1 = ( 1 , 3 , 5 )
test_tup2 = ( 4 , 6 )
print ( "The original tuple 1 : " + str (test_tup1))
print ( "The original tuple 2 : " + str (test_tup2))
res = test_tup1 + test_tup2
print ( "The tuple after concatenation is : " + str (res))
|
Output :
The original tuple 1 : (1, 3, 5)
The original tuple 2 : (4, 6)
The tuple after concatenation is : (1, 3, 5, 4, 6)
Time Complexity: O(N)
Auxiliary Space: O(N) where N is the lenght of the concatenate string.
Method #2 : Using sum() This is yet another way in which this task can be performed. In this, we add the tuples to one other using sum function.
Python3
test_tup1 = ( 1 , 3 , 5 )
test_tup2 = ( 4 , 6 )
print ( "The original tuple 1 : " + str (test_tup1))
print ( "The original tuple 2 : " + str (test_tup2))
res = sum ((test_tup1, test_tup2), ())
print ( "The tuple after concatenation is : " + str (res))
|
Output :
The original tuple 1 : (1, 3, 5)
The original tuple 2 : (4, 6)
The tuple after concatenation is : (1, 3, 5, 4, 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 #3: Using list() and extend() methods
Python3
test_tup1 = ( 1 , 3 , 5 )
test_tup2 = ( 4 , 6 )
print ( "The original tuple 1 : " + str (test_tup1))
print ( "The original tuple 2 : " + str (test_tup2))
x = list (test_tup1)
y = list (test_tup2)
x.extend(y)
res = tuple (x)
print ( "The tuple after concatenation is : " + str (res))
|
Output
The original tuple 1 : (1, 3, 5)
The original tuple 2 : (4, 6)
The tuple after concatenation is : (1, 3, 5, 4, 6)
Method #4: Using itertools.chain() function
Algorithm
1. Import the itertools module.
2. Create the original tuples tuple1 and tuple2.
3. Use the itertools.chain() function to concatenate the tuples.
4. Convert the concatenated result to a tuple.
5. Print the concatenated tuple.
Python3
import itertools
tuple1 = ( 1 , 3 , 5 )
tuple2 = ( 4 , 6 )
tuple3 = tuple (itertools.chain(tuple1, tuple2))
print ( "Concatenated tuple:" , tuple3)
|
Output
Concatenated tuple: (1, 3, 5, 4, 6)
Time Complexity: O(len(tuple1) + len(tuple2)), The itertools.chain() function is a lazy iterator that does not actually create a new list or tuple. Instead, it yields the elements from each of the input iterables in sequence. Therefore, the time complexity of the itertools.chain() function is O(1), because it only takes a constant amount of time to set up the iterator. The conversion to a tuple takes linear time in the length of the concatenated iterable, which is O(len(tuple1) + len(tuple2)).
Auxiliary Space: O(len(tuple1) + len(tuple2)), because the itertools.chain() function does not create a new list or tuple, but only an iterator that yields the elements in sequence. The tuple() function creates a new tuple that stores the concatenated elements. Therefore, the space complexity of the approach is proportional to the size of the concatenated tuple.
Method #5: Using the tuple() constructor and the * operator
This method uses the tuple() constructor to create a new tuple by concatenating the two original tuples using the + operator, and then converts the result back to a tuple using the tuple() constructor.
Python3
test_tup1 = ( 1 , 3 , 5 )
test_tup2 = ( 4 , 6 )
print ( "The original tuple 1 : " + str (test_tup1))
print ( "The original tuple 2 : " + str (test_tup2))
res = tuple (test_tup1 + test_tup2)
print ( "The tuple after concatenation is : " + str (res))
|
Output
The original tuple 1 : (1, 3, 5)
The original tuple 2 : (4, 6)
The tuple after concatenation is : (1, 3, 5, 4, 6)
Time complexity: O(n) ,where n is the total size of the tuples being concatenated.
Auxiliary space: O(n), where n is the total size of the tuples being concatenated.
METHOD 6: Using the += operator and the += method:
APPROACH:
In this approach, we use the += operator to concatenate the second tuple to the first tuple. This modifies the first tuple in place. We can also use the += method of tuples to achieve the same result.
ALGORITHM:
1.Create the first tuple t1.
2.Create the second tuple t2.
3.Use the += operator to concatenate t2 to t1.
4.Print the concatenated tuple t1
Python3
t1 = ( 1 , 2 , 3 )
t2 = ( 4 , 5 , 6 )
t1 + = t2
print (t1)
|
Output
(1, 2, 3, 4, 5, 6)
O(n) time complexity and O(n) auxiliary space.
Approach#7: Using reduce
Use a lambda function with reduce() function to concatenate two tuples.
Algorithm
1. Define a lambda function that takes two tuples as input and reduces them by concatenation using the + operator.
2. Call the lambda function with the two input tuples as arguments using the reduce() function.
3. Return the concatenated tuple.
Python3
from functools import reduce
concat_tuples = lambda t1, t2: reduce ( lambda x, y: x + y, [t1, t2])
t1 = ( 1 , 3 , 5 )
t2 = ( 4 , 6 )
concatenated_tuple = concat_tuples(t1, t2)
print (concatenated_tuple)
|
Time Complexity: O(n) where n is the total number of elements in the two tuples being concatenated.
Space Complexity: O(n) where n is the total number of elements in the two tuples being concatenated.
Please Login to comment...