Well, if you want an instant reaction from someone skimming this thread: I looked at the first example and couldn't understand it.>>> [prev * k for k in [5, 2, 4, 3] from prev = 1]
[1, 5, 10, 40, 120]
That makes sense for me, and seem simpler than:
>>> from itertools import accumulate, chain
>>> list(accumulate(chain([1], [5, 2, 4, 3]), lambda prev, k: prev * k))
[1, 5, 10, 40, 120]
from operator import mul
from itertools import accumulate, chain
accumulate(chain([1], nums), mul)
accumulate(nums, mul)
def prepend(item, it):
return itertools.chain([item], it)
def prepend(item, it):
yield item
yield from it