Search code examples
pythonpython-3.xfunctional-programmingmapreducepython-itertools

summing nested iterables with map-reduce/itertools


I've been stuck with this data structure for a while now:

iter([iter([1,0]),iter([1,1]),iter([0,0])])

I want to get to sum of the inner-most elements using map-reduce/itertools.

I am able to get to the answer fairly quickly using for loops:

outer_iter = iter([iter([1,0]),iter([1,1]),iter([0,0])])

for inner_iter in outer_iter:
    for inner_list in inner_iter:
        total = total + inner_list

I am struggling to "translate" the code.


Solution

  • If the data is nested two levels deep, We can use the chain function to concatenate the iterables together, and then let sum(..) calculate the sum of the iterable. So:

    from itertools import chain
    
    sum(chain.from_iterable(outer_iter))

    chain.from_iterable takes as input an iterable of iterables, and converts this into an iterable that lazily obtains the elements from the iterables one at a time. We can use iterable unpacking on chain, but if the outer iterable is an infinite list, the algorithm would get stuck (and eventually run out of memory).