How to use accumulate() to produce a cumulative sum of a series of numbers in Python

1 Answer

0 votes
from itertools import *    
 
# {a,b,c,...}, are a, a+b, a+b+c
 
print(list(accumulate(range(7)))) # 0,1,2,3,4,5,6
 
# 0 : 0+1=1 : 0+1+2=3 : 0+1+2+3=6 : 0+1+2+3+4=10 : 0+1+2+3+4+5=15 ...
 
 
 
'''
run:
 
[0, 1, 3, 6, 10, 15, 21]
  
'''

 



answered May 24, 2019 by avibootz
edited May 25, 2019 by avibootz

Related questions

...