Python concatenate tuples with examples

In this python tutorial, we will discuss the Python concatenate tuples and also we will cover these below topics:

  • Python concatenate tuples in a list
  • Python concatenate tuples
  • Python concatenate two tuples
  • Python concatenate string tuples
  • Python concatenate tuples to nested tuples
  • Python concatenate consecutive elements in tuples

Python concatenate tuples

Let us see how to concatenate tuples in python.

To concatenate tuples we will use ” sum ” function, and it will concatenate the tuples values.

Example:

my_tuple1 = (10, 20, 30)
my_tuple2 = (40, 50, 60)
res = sum((my_tuple1, my_tuple2), ()) 
print(res)

To get the output, I have used print(res). You can refer to the below screenshot for the output.

Python concatenate tuples
Python concatenate tuples

This is how we can concatenate tuples in Python.

Python concatenate tuples in a list

Here, we will see how to concatenate tuples in a list.

The join() function is used to join each tuple element with each other, and list comprehension is used to handle the task of iteration through tuples.

Example:

my_list = [('Python', 'Guides'),('learning', 'sites')]
s = [' '.join(tup) for tup in my_list] 
print ("The tuple in list is : " +  str(s)) 

To get the output, I have used print(“The tuple in list is: ” + str(s)). You can refer to the below screenshot for the output.

Python concatenate tuples in a list
Python concatenate tuples in a list

This is how to concatenate tuples in a list in Python.

You may also like, Python string formatting with examples and How to subtract two numbers in Python.

Python concatenate two tuples

Now, we will see python concatenate two tuples.

To concatenate two tuples we will use ” + ” operator to concatenate in python.

Example:

my_tuple1 = ('New York', 'Texas', 'Houston')
my_tuple2 = ('Chicago', 'California')
res = my_tuple1 + my_tuple2
print(res)

To get the output, I have used print(res). The output will come like:

('New York', 'Texas', 'Houston', 'Chicago', 'California')

This is how we can concatenate two tuples in Python.

Python concatenate string tuples

Now, we will see how to concatenate strings tuples.

In this example, we will use concat for extending the logic of concatenation, and mapping is done by using the map(). In this way, we will perform python concatenate string tuples.

Example:

from operator import concat  
my_tuple1 = ("Naveen", "Nitin", "Sagar")
my_tuple2 = ("Kriti", "Soha", "Mrinaz")
res = tuple(map(concat, my_tuple1, my_tuple2))  
print(res) 

To get the output, I have used print(res). You can refer to the below screenshot for the output.

Python concatenate string tuples
Python concatenate string tuples

This is how we can concatenate string tuples in Python.

Python concatenate tuples to nested tuples

Here, we will see how to concatenate tuples to nested tuples in Python.

To concatenate tuples to nested tuples we will perform the usual addition in the tuple elements. For initializing the tuples, we have to add a comma after the tuple.

Example:

my_tuple1 = (101, 102),
my_tuple2 = (104, 105),
res = my_tuple1 + my_tuple2
print(res) 

To get the output, I have used print(res). You can refer to the below screenshot for the output.

Python concatenate tuples to nested tuples
Python concatenate tuples to nested tuples

We checked here, how to concatenate tuples to nested tuples in Python.

Python concatenate consecutive elements in tuples

Let’s see how to concatenate consecutive elements in tuples.

To concatenate consecutive elements in tuples we will use a generator expression to provide concatenation logic and “zip()” is used for pairing. The result will be converted to tuple form using tuple().

Example:

my_tuple = ("Python ", "Guides ", "Is ", "Best ", "For ", "All ")  
res = tuple(i + j for i, j in zip(my_tuple, my_tuple[1:])) 
print(res)

To get the output, I have used print(res). You can refer to the below screenshot for the output.

Python concatenate consecutive elements in tuples
Python concatenate consecutive elements in tuples

This is how we can concatenate consecutive elements in tuples in Python.

You may like the following Python tutorials:

In this tutorial, we have learned about Python concatenate tuples, and also we have covered these topics:

  • Python concatenate tuples in a list
  • Python concatenate tuples
  • Python concatenate two tuples
  • Python concatenate string tuples
  • Python concatenate tuples to nested tuples
  • Python concatenate consecutive elements in tuples