Python | Ways to concatenate tuples
Last Updated :
12 Jul, 2025
In Python, concatenating tuples means joining two or more tuples into a single tuple. Since tuples are immutable (cannot be changed after creation), concatenation is the way to combine their elements without altering original tuples.
Example:
Input: t1 = (1,2), t2 = (3,4)
Output: T = (1,2,3,4)
Let's discuss ways to concatenate tuples.
Using + operator
+ operator is a binary operator used to add values and when applied to tuples, it performs concatenation. This means it joins two tuples, creating a new tuple that contains all the elements from both tuples.
Example:
Python
t1 = (1, 3, 5)
t2 = (4, 6)
T = t1 + t2
print(T)
itertools.chain() function from itertools module is used to iterate over multiple sequences as if they were a single continuous sequence. When applied to tuples it acts like a chain, linking all elements from each tuple together.
Example:
Python
import itertools
t1 = (1, 3, 5)
t2 = (4, 6)
T = tuple(itertools.chain(t1, t2))
print(T)
Explanation:
- itertools.chain(t1, t2): creates an iterator that yields all elements from t1 followed by all elements from t2.
- tuple(): converts the chained iterator into a single concatenated tuple.
Using sum()
sum() function is used for adding numbers, but it can also concatenate multiple tuples when given a list of tuples and an empty tuple () as the starting value. It works by treating the tuples like elements in a sequence and adding them together one by one.
Example:
Python
t1 = (1, 3, 5)
t2 = (4, 6)
T = sum((t1, t2), ())
print(T)
Explanation: sum((t1, t2), ()) adds the tuples together starting from an empty tuple () effectively concatenating them.
Using list() + extend() methods
This method converts tuples to lists, merges them using extend() and then converts the result back to a tuple. It's a practical approach using list mutability.
Example:
Python
t1 = (1, 3, 5)
t2 = (4, 6)
x = list(t1)
y = list(t2)
x.extend(y)
T = tuple(x)
print(T)
Explanation:
- list(t1) and list(t2) turn tuples into lists.
- x.extend(y) appends all elements of y to x.
- tuple(x) converts the merged list back to a tuple.
Using tuple() constructor + * operator
Unpacking operator (*) along with the tuple() constructor combines multiple tuples into a single tuple. It's a clean approach that creates a new concatenated tuple without altering the original ones.
Example:
Python
t1 = (1, 3, 5)
t2 = (4, 6)
T = tuple((*t1, *t2))
print(T)
Explanation:
- *t1 and *t2 unpack the elements of two tuples.
- (*t1, *t2) creates a new tuple by combining all unpacked elements.
- tuple() ensures result is a tuple.
Using reduce
reduce() function from functools module is used to concatenate multiple tuples by applying + operator repeatedly. This method is useful when combining more than two tuples efficiently in a single step.
Example:
Python
from functools import reduce
t1 = (1, 2)
t2 = (3, 4)
t3 = (5, 6)
T = reduce(lambda x, y: x + y, [t1, t2, t3])
print(T)
Explanation: reduce(lambda x, y: x + y, [t1, t2, t3]) combines all tuples in a list [t1, t2, t3] using a lambda function that adds them together.
Related Articles: