Different ways to concatenate Python tuples

Tuple Python

In this post, we are going to explore how to use Different ways to concatenate Python tuples that can be single or multiple tuples. Sometimes while data manipulation we need to join tuples to perform some operation. We will explore how to concatenate tuples into single tuple or nested tuples.

Ways to concatenate tuples to single or nested tuples.


  • Sum(): This is a built-in function that we can use to concatenate tuples into single/nested Tuples.
  • + Operator: To concatenate tuples into single/nested Tuples.

1. How to nest two Tuples in Python


To Python concatenate tuples elements to a single Tuple we will use the + operator and sum() function.

  • Using sum() function
  • Using + operator

1.1 Python Concatenate tuple element to single Tuple


In our first example, we will use the in-built sum() function to concatenate two tuples to a single tuple. It can be used to concatenate number and string-type tuples. and result into a single tuple. Let us jump to an example:

Program Example

original_tuple1= (1,2,3,4,5,6)

original_tuple2 = ('C#','C++','Python','Go')

print('original tuple1 : ',original_tuple1,'\noriginal  tuple2 :',original_tuple2 )


#concatenate the tuple Using sum()


result_concate_tuple = sum((original_tuple1, original_tuple2), ()) 

print('\n The tuple after concatenation \n  =',result_concate_tuple )

Output:

original tuple1 :  (1, 2, 3, 4, 5, 6) 
original  tuple2 : ('C#', 'C++', 'Python', 'Go')

 The tuple after concatenation 
  = (1, 2, 3, 4, 5, 6, 'C#', 'C++', 'Python', 'Go')

1.2. +operator to concatenate single Tuple


In our first example, we will use the “+” operator to concatenate two tuples and result in a single tuple. In this example we have two tuples original_tuple1 and original_tuple2, We are concatenating these tuples into a single tuple as we can see in the output.

Let us check in example:

Code example

original_tuple1= (1,2,3,4,5,6)

original_tuple2 = ('C#','C++','Python','Go')

print('original tuple1 : ',original_tuple1,'\noriginal  tuple2 :',original_tuple2 )


#concatenate the tuple Using sum()

result_concate_tuple = original_tuple1+original_tuple2

print('\n The tuple after concatenation \n  =',result_concate_tuple )

Output:

original tuple1 :  (1, 2, 3, 4, 5, 6) 
original  tuple2 : ('C#', 'C++', 'Python', 'Go')

 The tuple after concatenation 
  = (1, 2, 3, 4, 5, 6, 'C#', 'C++', 'Python', 'Go')

2. How to concatnate Tuple to nested Tuple


  • Using Sum() and comma(,) during initialization of tuples.
  • ‘+’ with,operator to concate to nested Tuples
  • comma(,) during concatenation to nested tuple

2.1 Sum to concatenate tuple to nested tuple


Now let us understand how we can sum tuples to make a nested tuple. In this program we will use two tuples which are nested tuples, please note the ‘,’ at the end of each tuple original_tuple1and original_tuple2.

# comma(,) after each tuple to concatenate nested tuple
original_tuple1= (1,2,3,4,5,6),

original_tuple2 = ('C#','C++','Python','Go'),

print('original tuple1 : ',original_tuple1,'\noriginal  tuple2 :',original_tuple2 )


#concatenation of  tuples to nested tuple Using sum()

result_concate_tuple = sum((original_tuple1, original_tuple2), ()) 

print('\n The tuple after concatenation \n  =',result_concate_tuple )



Output:

original tuple1 :  ((1, 2, 3, 4, 5, 6),) 
original  tuple2 : (('C#', 'C++', 'Python', 'Go'),)

 The tuple after concatenation 
  = ((1, 2, 3, 4, 5, 6), ('C#', 'C++', 'Python', 'Go'))

2.2 ‘+’ with,operator to concate to nested Tuples


In this example, we have two tuples original_tuple1 and original_tuple2. We are using comma(,) end of each tuple to concatenate them into a nested tuple. We are concatenating these tuples into a nested tuple as we can see in the resulting output.

Code example

# comma(,) during initlization of tuples to concatenate nested tuple

original_tuple1= (1,2,3,4,5,6),

original_tuple2 = ('C#','C++','Python','Go'),

print('original tuple1 : ',original_tuple1,'\noriginal  tuple2 :',original_tuple2 )


#concatenate the tuple Using sum()

result_concate_tuple = original_tuple1+original_tuple2

print('\n The tuple after concatenation \n  =',result_concate_tuple )

Output:

original tuple1 :  ((1, 2, 3, 4, 5, 6),) 
original  tuple2 : (('C#', 'C++', 'Python', 'Go'),)

 The tuple after concatenation 
  = ((1, 2, 3, 4, 5, 6), ('C#', 'C++', 'Python', 'Go'))

2.3 comma(,) during concatenation to nested tuple


In this example, we will learn with example how to concatenate tuples to nested tuples by using comma during concatenation of tuples.

Program concatenate tuple to nested tuple


# comma(,) during concatenation to nested tuple

original_tuple1= (1,2,3,4,5,6)

original_tuple2 = ('C#','C++','Python','Go')

print('original tuple1 : ',original_tuple1,'\noriginal  tuple2 :',original_tuple2 )


#concatenate the tuples to nested Tuples

nested_tuples = ((original_tuple1, ) + (original_tuple2, ))
  

print('\n The tuple after concatenation \n  =',nested_tuples )

Output

original tuple1 :  (1, 2, 3, 4, 5, 6) 
original  tuple2 : ('C#', 'C++', 'Python', 'Go')

 The tuple after concatenation 
  = ((1, 2, 3, 4, 5, 6), ('C#', 'C++', 'Python', 'Go'))

Summary

in this post, we have learned Different ways to concatenate Python tuples