Open In App
Related Articles

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.

step-by-step approach :

1. Initialize two tuples with elements (1, 3, 5) and (4, 6) respectively and assign them to the variables test_tup1 and test_tup2.
2. Print the original values of the two tuples using print() function and concatenate them with the string using + operator.
3. Concatenate the two tuples test_tup1 and test_tup2 using + operator and assign the result to a new variable res.
4. Print the concatenated tuple res using print() function and concatenate it with the string using + operator.
5. End of the program.

Python3

 # Python3 code to demonstrate working of# Ways to concatenate tuples# using + operator # initialize tuplestest_tup1 = (1, 3, 5)test_tup2 = (4, 6) # printing original tuplesprint("The original tuple 1 : " + str(test_tup1))print("The original tuple 2 : " + str(test_tup2)) # Ways to concatenate tuples# using + operatorres = test_tup1 + test_tup2 # printing resultprint("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

 # Python3 code to demonstrate working of# Ways to concatenate tuples# using sum() # initialize tuplestest_tup1 = (1, 3, 5)test_tup2 = (4, 6) # printing original tuplesprint("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 resultprint("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

 # Python3 code to demonstrate working of# Ways to concatenate tuples # initialize tuplestest_tup1 = (1, 3, 5)test_tup2 = (4, 6) # printing original tuplesprint("The original tuple 1 : " + str(test_tup1))print("The original tuple 2 : " + str(test_tup2)) # Ways to concatenate tuplesx = list(test_tup1)y = list(test_tup2)x.extend(y)res = tuple(x) # printing resultprint("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)

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 tuplestuple1 = (1, 3, 5)tuple2 = (4, 6) # concatenate the tuples using itertools.chain()tuple3 = tuple(itertools.chain(tuple1, tuple2)) # print the concatenated tupleprint("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

 # Python3 code to demonstrate working of# Ways to concatenate tuples# using * operator # initialize tuplestest_tup1 = (1, 3, 5)test_tup2 = (4, 6) # printing original tuplesprint("The original tuple 1 : " + str(test_tup1))print("The original tuple 2 : " + str(test_tup2)) # Ways to concatenate tuples# using * operator and tuple() constructorres = tuple(test_tup1 + test_tup2) # printing resultprint("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 += t2print(t1)  # Output: (1, 2, 3, 4, 5, 6)

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 reduceconcat_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)

Output

(1, 3, 5, 4, 6)

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.