Python | Ways to concatenate tuples
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.
Python3
# Python3 code to demonstrate working of # Ways to concatenate tuples # using + operator # initialize tuples test_tup1 = ( 1 , 3 , 5 ) test_tup2 = ( 4 , 6 ) # printing original tuples print ("The original tuple 1 : " + str (test_tup1)) print ("The original tuple 2 : " + str (test_tup2)) # Ways to concatenate tuples # using + operator res = test_tup1 + test_tup2 # printing result print ("The tuple after concatenation is : " + str (res)) |
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
# Python3 code to demonstrate working of # Ways to concatenate tuples # using sum() # initialize tuples test_tup1 = ( 1 , 3 , 5 ) test_tup2 = ( 4 , 6 ) # printing original tuples print ("The original tuple 1 : " + str (test_tup1)) print ("The original tuple 2 : " + str (test_tup2)) # Ways to concatenate tuples # using sum() res = sum ((test_tup1, test_tup2), ()) # printing result print ("The tuple after concatenation is : " + str (res)) |
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
# Python3 code to demonstrate working of # Ways to concatenate tuples # initialize tuples test_tup1 = ( 1 , 3 , 5 ) test_tup2 = ( 4 , 6 ) # printing original tuples print ( "The original tuple 1 : " + str (test_tup1)) print ( "The original tuple 2 : " + str (test_tup2)) # Ways to concatenate tuples x = list (test_tup1) y = list (test_tup2) x.extend(y) res = tuple (x) # printing result print ( "The tuple after concatenation is : " + str (res)) |
The original tuple 1 : (1, 3, 5) The original tuple 2 : (4, 6) The tuple after concatenation is : (1, 3, 5, 4, 6)
Method #3 : 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
# Python program to concatenate tuples import itertools # original tuples tuple1 = ( 1 , 3 , 5 ) tuple2 = ( 4 , 6 ) # concatenate the tuples using itertools.chain() tuple3 = tuple (itertools.chain(tuple1, tuple2)) # print the concatenated tuple print ( "Concatenated tuple:" , tuple3) |
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 #4: 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
# Python3 code to demonstrate working of # Ways to concatenate tuples # using * operator # initialize tuples test_tup1 = ( 1 , 3 , 5 ) test_tup2 = ( 4 , 6 ) # printing original tuples print ( "The original tuple 1 : " + str (test_tup1)) print ( "The original tuple 2 : " + str (test_tup2)) # Ways to concatenate tuples # using * operator and tuple() constructor res = tuple (test_tup1 + test_tup2) # printing result print ( "The tuple after concatenation is : " + str (res)) |
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.
Please Login to comment...