def original_approach(a, b):
    c = []
d = 0
for i, val in enumerate(a):
    d = d + val
c.append(d)
d = d * b[i]
return c

def vectorized_approach(a, b):
    b_rev_cumprod = b[::-1].cumprod()[::-1]
B = np.hstack((b_rev_cumprod, 1))
K = a * b
K_ext = np.hstack((0, K))
sums = (B * K_ext).cumsum()
return sums[1:] / b_rev_cumprod
Cumulative addition/multiplication in NumPy