Python Practice › Tuple Programs › Program 45

Element-wise sum of tuples

EasyTuple Programs · 45 of 70

Write a Python program to add several tuples element by element.

Advertisement
Input
(1, 6, 7), (4, 3, 0), (2, 6, 8)
Output
(7, 15, 15)

Approach

  1. zip(*tuples) groups items by position; sum each group.

Solution

Python
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.

More tuple programs

← All 70 Tuple Programs

Leave a Comment