Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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) # Expected Output: (14, 20, 30, 45, 12) # The corresponding elements are added: # 3 + 11 = 14, 6 + 14 = 20, 9 + 21 = 30, 45 + 0 = 45, 6 + 6 = 12
Adding Corresponding Elements of Tuples
There are several approaches to add corresponding elements of two tuples. The methods include ?
- Using for loop
- Using map() and add()
- Using zip()
- Using numpy library
Using for Loop
Follow these steps to add corresponding elements using a for loop ?
- Run a for loop through the tuples and add elements at corresponding indices
- Append the sum to a list using append() function
- Convert the list to a tuple after the loop completes
Example
The code below shows how to add tuples using a for loop ?
# Input tuples
tup1 = (7, 5.7, 21, 18, 8/3)
tup2 = (9, 15, 6.2, 1/3, 11)
result_list = []
for i in range(0, len(tup1)):
result_list.append(tup1[i] + tup2[i])
result_tuple = tuple(result_list)
print(result_tuple)
The output of the above code is ?
(16, 20.7, 27.2, 18.333333333333332, 13.666666666666666)
Using map() and add()
The map() function along with add() can find the sum of corresponding elements. The map() function takes the add function and two tuples as parameters ?
Example
The code below demonstrates how to add tuples using map() function ?
from operator import add tup1 = (7, 5.7, 21, 18, 8/3) tup2 = (9, 15, 6.2, 1/3, 11) result_tuple = tuple(map(add, tup1, tup2)) print(result_tuple)
The output of the above code is ?
(16, 20.7, 27.2, 18.333333333333332, 13.666666666666666)
Using zip()
The zip() function aggregates elements from two tuples. First, zip() pairs corresponding elements, then sum() adds each pair ?
Example
The code below shows how to use zip() function to add tuples ?
# Input tuples tup1 = (7, 5.7, 21, 18, 8/3) tup2 = (9, 15, 6.2, 1/3, 11) result = tuple(sum(pair) for pair in zip(tup1, tup2)) print(result)
The output of the above code is ?
(16, 20.7, 27.2, 18.333333333333332, 13.666666666666666)
Using NumPy Library
NumPy provides the add() function for element-wise addition. The np.add() function automatically handles tuple conversion internally ?
Example
The code below shows how to use numpy library to add tuple elements ?
import numpy as np # Input tuples tup1 = (1, 3, 4, 6, 8) tup2 = (4, 5, 6, 2, 10) # Add tuples using numpy and convert back to tuple result_tuple = tuple(np.add(tup1, tup2)) print(result_tuple)
The output of the above code is ?
(5, 8, 10, 8, 18)
Comparison
| Method | Best For | Performance |
|---|---|---|
| for loop | Small tuples, learning | Moderate |
| map() + add() | Functional programming | Good |
| zip() | Readable, Pythonic | Good |
| NumPy | Large datasets, scientific computing | Excellent |
Conclusion
Use zip() for readable Pythonic code, map() for functional programming, and NumPy for large datasets. The for loop approach is best for beginners to understand the underlying logic.
