Addition of tuples in Python



In Python, tuples are used to store an immutable sequence of elements. In this article, we will learn different methods to implement a Python program to add corresponding elements of tuples.

Here, you are given two equally sized tuples in Python and your task is to create a new tuple containing sum of corresponding elements of the tuples.

Consider the following example scenario:

Scenario

Input Tuples:
tup1 = (3, 6, 9, 45, 6)
tup2 = (11, 14, 21, 0, 6)

Output:
 (14, 20, 30, 45, 12 )

Explanation:
The corresponding elements that are added to get the output are:
3 + 11 = 14, 6 + 14 = 20, 9 + 21 = 30 and so on.

Adding Corresponding Elements of Tuples

There are several approaches to add corresponding elements of two tuples. Depending upon the context and your convenience, you can use the method most suitable for you. In short, the methods can be listed as:

Add Tuple Elements Using a for Loop

Follow the steps below to add corresponding elements of tuples using a for loop:

  • First, we will run a for loop through the tuples (say, tup1 and tup2) and add elements at the corresponding index using tup1[i] + tup2[i].
  • The sum of this addition will be inserted into a list using append() function.
  • After completing the loop, the list will be converted to tuple and printed to the console. Let's look at an example for this.

Example

The code below shows how to add tuples using a for loop and append().

# Input tuples
tup1 = (7, 5.7, 21, 18, 8/3)
tup2 = (9, 15, 6.2, 1/3,11)

newlist = []
for i in range(0, len(tup1)):
   newlist.append(tup1[i] + tup2[i])

tup = tuple(newlist)
print(tup)

Running the above code gives us the following result:

(16, 20.7, 27.2, 18.333333333333332, 13.666666666666666)

Using map() and add() to Add Tuple Elements

We can use the map() along with the add() to find the sum of the corresponding elements of the input tuples. The map() function takes the add function and two tuples as parameters. The result of the map() function will be wrapped in a tuple function to store values as a tuple.

Example

In the code below, we will demonstrate how to add tuples using the map() function:

from operator import add
tup1 = (7, 5.7, 21, 18, 8/3)
tup2 = (9, 15, 6.2, 1/3,11)
newtup = tuple(map(add, tup1, tup2))
print(newtup)

Running the above code gives us the following result:

(16, 20.7, 27.2, 18.333333333333332, 13.666666666666666)

Add Tuple Elements Using Zip()

The zip() function in Python is used to aggregate elements of two different lists, tuples, etc. To add tuple elements, first we use the zip() function to make a pair of corresponding elements of two tuples. Then, the pair value can be added using the sum() function.

Example

The code below shows, how to use zip() function to add tuples:

#Input tuple
tup1 = (7, 5.7, 21, 18, 8/3)
tup2 = (9, 15, 6.2, 1/3,11)

result = tuple(sum(n) for n in zip(tup1, tup2))
print(result)

Running the above code gives us the following result:

(16, 20.7, 27.2, 18.333333333333332, 13.666666666666666)

Add Tuple Elements Using NumPy Library

The NumPy library is an array manipulation library in Python. It provides the add() function, which performs element-wise addition. To add corresponding elements of tuples, you can first convert the tuples into NumPy arrays and then use np.add() to compute their sum.

Note that, the np.add() function automatically converts the parameters into NumPy arrays implicitly, before performing element-wise addition.

Example

The code below shows how to use numpy library to add tuple elements:

# Import numpy library
import numpy as np

# Input tuple
a = (1, 3, 4, 6, 8)
b = (4, 5, 6, 2, 10)

# Convert tuples to numpy arrays and add them
c = tuple(np.add(a, b))
# Print the result
print(c)

Running the above code gives us the following result:

(5, 8, 10, 8, 18)

Conclusion

In this article, we explored four different methods to add corresponding elements of two equal-sized tuples in Python. You can choose the most suitable method by considering the context and complexity of your task. If you are handling large arrays like 2D, 3D, then numPy method will be best, and for small arrays you can use a simple for loop or zip function.

Farhan Muhamed
Farhan Muhamed

No Code Developer, Vibe Coder

Updated on: 2025-07-14T18:54:28+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements