Python Practice › Tuple Programs › Program 45
Element-wise sum of tuples
Write a Python program to add several tuples element by element.
Input
(1, 6, 7), (4, 3, 0), (2, 6, 8)
Output
(7, 15, 15)
Approach
zip(*tuples)groups items by position; sum each group.
Solution
tuples = [(1, 6, 7), (4, 3, 0), (2, 6, 8)]print(tuple(sum(column) for column in zip(*tuples)))Output
(7, 15, 15)
Line by line
Line 2
zip(*tuples) gives (1, 4, 2), (6, 3, 6) and (7, 0, 8).
Learn the concept
New to this topic? Read the lesson, then come back.