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.
Similar Reads
Python Tuples
A tuple in Python is an immutable ordered collection of elements. Tuples are similar to lists, but unlike lists, they cannot be changed after their creation (i.e., they are immutable). Tuples can hold elements of different data types. The main characteristics of tuples are being ordered , heterogene
7 min read
Tuple Operations in Python
Python Tuple is a collection of objects separated by commas. A tuple is similar to a Python list in terms of indexing, nested objects, and repetition but the main difference between both is Python tuple is immutable, unlike the Python list which is mutable. [GFGTABS] Python # Note : In case of list,
7 min read
Create a List of Tuples in Python
The task of creating a list of tuples in Python involves combining or transforming multiple data elements into a sequence of tuples within a list. Tuples are immutable, making them useful when storing fixed pairs or groups of values, while lists offer flexibility for dynamic collections. For example
3 min read
Create a tuple from string and list - Python
The task of creating a tuple from a string and a list in Python involves combining elements from both data types into a single tuple. The list elements are added as individual items and the string is treated as a single element within the tuple. For example, given a = ["gfg", "is"] and b = "best", t
3 min read
Access front and rear element of Python tuple
Sometimes, while working with records, we can have a problem in which we need to access the initial and last data of a particular record. This kind of problem can have application in many domains. Let's discuss some ways in which this problem can be solved. Method #1: Using Access Brackets We can pe
6 min read
Python - Element Index in Range Tuples
Sometimes, while working with Python data, we can have a problem in which we need to find the element position in continuous equi ranged tuples in list. This problem has applications in many domains including day-day programming and competitive programming. Let's discuss certain ways in which this t
6 min read
Unpacking a Tuple in Python
Tuple unpacking is a powerful feature in Python that allows you to assign the values of a tuple to multiple variables in a single line. This technique makes your code more readable and efficient. In other words, It is a process where we extract values from a tuple and assign them to variables in a s
2 min read
Unpacking Nested Tuples-Python
The task of unpacking nested tuples in Python involves iterating through a list of tuples, extracting values from both the outer and inner tuples and restructuring them into a flattened format. For example, a = [(4, (5, 'Gfg')), (7, (8, 6))] becomes [(4, 5, 'Gfg'), (7, 8, 6)]. Using list comprehensi
3 min read
Python | Slice String from Tuple ranges
Sometimes, while working with data, we can have a problem in which we need to perform the removal from strings depending on specified substring ranges. Let's discuss certain ways in which this task can be performed. Method #1: Using loop + list slicing: This is the brute force task to perform this t
3 min read
Python - Clearing a tuple
Sometimes, while working with Records data, we can have a problem in which we may require to perform clearing of data records. Tuples, being immutable cannot be modified and hence makes this job tough. Let's discuss certain ways in which this task can be performed. Method #1 : Using list() + clear()
4 min read