Write a Python program to compute element-wise sum of given tuples


The program is written in Python and it calculates the element-wise sum of multiple tuples. The program creates four tuples a, b, c, and d each containing 3 elements. The program then uses the map() function and the zip() function to perform element-wise addition of the tuples. The zip() function combines the elements of the four tuples into one tuple of tuples, with each inner tuple containing the corresponding elements from each of the four tuples. The map() function applies the sum() function to each inner tuple to calculate the element-wise sum. Finally, the program converts the resulting map object to a tuple and prints the result using the print() function.

Sample tuple :
(2, 5, 8)
(6, 5, 1)
(1, 4, 9)
(3, 7, 2)
Sum of Elements :(6, 9, 8, 6)

Source Code

a = (2, 5, 8)
b = (6, 5, 1)
c = (1, 4, 7)
d = (3, 7, 2)
print("A :",a)
print("B :",b)
print("C :",c)
print("D :",d)
res = tuple(map(sum, zip(a, b, c, d)))
print("Sum of Elements:",res)
 

Output

A : (2, 5, 8)
B : (6, 5, 1)
C : (1, 4, 7)
D : (3, 7, 2)
Sum of Elements: (12, 21, 18)

Example Programs