list_ = [(1, 2), (3, 4)]
What is the Pythonic way of taking sum of ordered pairs from inner tuples and multiplying the sums? For the above example:
(1 + 3) * (2 + 4) = 24
For example:
import operator as op
import functools
functools.reduce(op.mul, (sum(x) for x in zip(*list_)))
works for any length of the initial array as well as of the inner tuples.
Another solution using numpy:
import numpy as np
np.array(list_).sum(0).prod()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With