Open In App

Position Summation in List of Tuples - Python

Last Updated : 21 Jan, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

Position Summation in List of Tuples refers to the process of calculating the sum of elements at the same positions across multiple tuples in a list. This operation involves adding up the corresponding elements from each tuple.

For example, consider the list of tuples [(1, 6), (3, 4), (5, 8)]. The goal is to sum all the first elements together and all the second elements together. The result of this position summation would be (9, 18), where 9 is the sum of the first elements (1 + 3 + 5) and 18 is the sum of the second elements (6 + 4 + 8).

Using zip()

zip function when used with unpacking (*) groups elements from each tuple based on their respective positions. By using the map function with sum, we calculate the sum of each grouped tuple efficiently in a single pass.

Python
li = [(1, 6), (3, 4), (5, 8)]
res = tuple(map(sum, zip(*li)))
print(str(res))

Output
(9, 18)

Explanation:

  • zip(*li): * operator unpacks li into individual tuples and zip then groups elements at the same positions .
  • map(sum, ...): This applies sum to each group of li .
  • tuple(): This converts the result into a tuple .

Using Numpy arrays

For large datasets, NumPy provides highly optimized array operations that significantly improve performance. By converting a list of tuples into a NumPy array and using the np.sum() function we can efficiently compute position wise summation.

Python
import numpy as np
li = [(1, 6), (3, 4), (5, 8)]
res = tuple(np.sum(np.array(li), axis=0))
print(str(res))

Output
(np.int64(9), np.int64(18))

Explanation:

  • np.array(li) converts the list of tuples li into a NumPy array, enabling efficient numerical operations.
  • np.sum() This sums elements along the columns .
  • tuple(np.sum(...)) This converts the result from NumPy array to a Python tuple.

Using reduce

reduce () from functools efficiently performs pairwise computation across tuples by applying a summation to aggregate values. It directly combines results during iteration, avoiding the need for intermediate structures and making it ideal for large datasets.

Python
from functools import reduce
li= [(1, 6), (3, 4), (5, 8)]
res = reduce(lambda acc, ele: (acc[0] + ele[0], acc[1] + ele[1]),li)
print(str(res))

Output
(9, 18)

Explanation:

  • reduce: This applies the lambda function cumulatively to the list and starting with the first tuple.
  • lambda function: This sums the first elements and second elements separately across all tuples.

Using for loop

Using a for loop for position wise summation is simple but it may be slower for large datasets due to Python's interpreted nature, lacking the optimizations of methods like NumPy or reduce. It's efficient for smaller datasets but less so for larger ones.

Python
li = [(1, 6), (3, 4), (5, 8)]

# Manual summation
sum1, sum2 = 0, 0
for x, y in li:
    sum1 += x
    sum2 += y

res = (sum1, sum2)
print(str(res))

Output
(9, 18)

Explanation:

  • for x, y in li: This loops through each tuple, unpacking it into x and y.
  • sum1 += x: This adds x to sum1.
  • sum2 += y: This adds y to sum2.

Next Article

Similar Reads