We can easily concatenate tuples in Python. To concatenate the elements of two tuples, you can use the Python + operator.

tuple1 = (1, 2, 3)
tuple2 = (4, 5)

print(tuple1 + tuple2)

#Output:
(1, 2, 3, 4, 5)

You can also use the Python sum() function to concatenate tuples.

tuple1 = (1, 2, 3)
tuple2 = (4, 5)

tuple3 = sum((tuple1,tuple2),())

#Output:
(1, 2, 3, 4, 5)

In Python, tuples are a collection of objects which are ordered and mutable. When working with a list of tuples, it can be useful to be able to concatenate and add tuples together.

We can easily add tuples together in Python.

To add two tuples together, we can use the Python + addition operator.

Just like with other collections of data in Python, + adds the elements of another tuple to the end of the first tuple.

Below is a simple Python example showing how to concatenate two tuples.

tuple1 = (1, 2, 3)
tuple2 = (4, 5)

print(tuple1 + tuple2)

#Output:
(1, 2, 3, 4, 5)

Concatenating Tuples Using Python sum() Function

Another useful function in Python is sum(). The Python sum() function takes two arguments – an iterable object which is required, and an optional starting point.

We can use the sum() function to concatenate the elements of two tuples together easily. To append the elements of one tuple to another tuple using sum(), we just pass an empty tuple to sum() for the starting value.

Below is an example in Python of how to concatenate tuples using sum().

tuple1 = (1, 2, 3)
tuple2 = (4, 5)

tuple3 = sum((tuple1,tuple2),())

#Output:
(1, 2, 3, 4, 5)

Hopefully this article has been useful for you to learn how to concatenate the elements of tuples in Python.

Categorized in:

Python,

Last Update: February 26, 2024