Q:

Using reduce to shorten a for-loop

From an array Ns, I'd like to derive an array multipliers as follows:
Ns = [3, 3, 6, 3]

multipliers = [0]*len(Ns)
multipliers[0] = 1
for n in range(1,len(Ns)):
    multipliers[n] = multipliers[n-1] * Ns[n-1]

The resulting array multipliers is [1, 3, 9, 54]. My gut feeling is that it should be possible to make this code more succinct using reduce or another built-in function, but I don't yet see how. Any ideas?

A:

You could use itertools.accumulate with a custom accumulation function (Python 3 only, if you want to use Python 2 you could install fn.py library (or similar) or backport the function using the implementation provided in the docs) :
In [10]: from itertools import accumulate

In [11]: import operator

In [12]: list(accumulate([3, 3, 6, 3], func=operator.mul))
Out[12]: [3, 9, 54, 162]

And then just fix the first and the last elements:
In [13]: l = list(accumulate([3, 3, 6, 3], func=operator.mul))

In [14]: [1] + l[:-1]
Out[14]: [1, 3, 9, 54]

A:

You can simulate the behavior of accumulate (see @soon's answer) in Python 2 with reduce. You have to manage the list by yourself. 
from functools import reduce
Ns = [3, 3, 6, 3]
multipliers = reduce(lambda l, x: l + [l[-1] * x], Ns[:-1], [1])

