How does concatenation operator work on tuple in Python?


A tuple is a collection of python objects that are separated by commas which are ordered and immutable. Tuples are sequences, just like lists. The differences between tuples and lists are, that tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets.

tup=('tutorials', 'point', 2022,True)
print(tup)

If you execute the above snippet, produces the following output −

('tutorials', 'point', 2022, True)

In this article, we will look at how to concatenate operator works on tuples in python.

Concatenate operation on tuples

Concatenation of tuples in python means joining two or more tuples to form a single tuple. They are different ways to perform the concatenation of two tuples. Two of those methods are discussed below.

  • Using the ‘+’ operator.
  • Using the sum() function.

Using the ‘+’ operator

When multiple tuples must be concatenated, the '+' operator can be used. A tuple is an immutable data type. This indicates that once a value is defined, it cannot be altered by accessing its index elements. We get an error if we try to alter the elements. They're crucial since they guarantee read-only access.

The '+' operator can be used to concatenate texts or add numeric values.

Example

The below example demonstrates the use of + operator to concatenate tuples.

tuple_1 = (11, 14, 0, 78, 33, 11)
tuple_2 = (10, 78, 0, 56, 8, 34)
print("The first tuple is : ")
print(tuple_1)
print("The second tuple is : ")
print(tuple_2)
result = tuple_1 + tuple_2
print("The tuple after concatenation is : " )
print(result)

Output

The output of the above code is as follows;

The first tuple is : 
(11, 14, 0, 78, 33, 11)
The second tuple is : 
(10, 78, 0, 56, 8, 34)
The tuple after concatenation is : 
(11, 14, 0, 78, 33, 11, 10, 78, 0, 56, 8, 34)

Using the sum() function.

There is another way to solve this problem, we use the sum() function to concatenate tuples. The sum() function takes two tuples as an argument and returns a single tuple which is the concatenation of the two tuples.

Example

In the following example code, we concatenate two tuples using the sum() function.

tuple_1 = (11, 14, 0, 78, 33, 11)
tuple_2 = (10, 78, 0, 56, 8, 34)
print("The first tuple is : ")
print(tuple_1)
print("The second tuple is : ")
print(tuple_2)
result = sum((tuple_1, tuple_2), ())
print("The tuple after concatenation is : " )
print(result)

Output

The above code, produces the following output.

The first tuple is : 
(11, 14, 0, 78, 33, 11)
The second tuple is : 
(10, 78, 0, 56, 8, 34)
The tuple after concatenation is : 
(11, 14, 0, 78, 33, 11, 10, 78, 0, 56, 8, 34)

Using the list() and extend() methods

The extends() method of a List in python is used to add two lists. To concatenate two tuples using these methods

  • Convert the tuple into a list using the list() method.
  • Add these two lists using the extend() method.

Example

Following is an example to concatenate two arrays using list() and extend() methods −

tuple1 = ('JavaFX', 'OpenCV','CoffeeScript')
tuple2 = ('Hadoop', 'Spark')

print("Contents of tuple1 : " + str(tuple1))
print("Contents of tuple2 : " + str(tuple2))

list1=list(tuple1)
list1.extend(tuple2)
print("Result : " + str(list1))

Output

Contents of tuple1 : ('JavaFX', 'OpenCV', 'CoffeeScript')
Contents of tuple2 : ('Hadoop', 'Spark')Result : ['JavaFX', 'OpenCV', 'CoffeeScript', 'Hadoop', 'Spark']

Updated on: 05-Sep-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements