I'm trying to combine a bunch of items in a list to create a destination object, in a way that is similar to creating a sum of objects. I am trying to use a simple option on reduce where you are looking at a list of pairs, rather than a flat list, to do this. I want to do something like:
nums = [1, 2, 3] reduce(lambda x, y: x + y, nums)
except that I would like to add additional information to the amount that applies to each item in the nums list of numbers. For example, for each pair (a, b) in the list, execute the sum as (a + b):
nums = [(1, 0), (2, 5), (3, 10)] reduce(lambda x, y: (x[0]+x[1]) + (y[0]+y[1]), nums)
This does not work:
>>> reduce(lambda x, y: (x[0]+x[1]) + (y[0]+y[1]), nums) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 1, in <lambda> TypeError: 'int' object is unsubscriptable
Why is this not working? I know that I can encode nums as a flat list - this is not the main thing - I just want to be able to create a reduction operation that can iterate over a list of pairs or more than two lists of the same length at the same time and pool information from both lists. thanks.