why does accumulate increments value in defined manner(adding the previous list values only with natural numbers)? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

why does accumulate increments value in defined manner(adding the previous list values only with natural numbers)?

Is there alternate for it?

1st Sep 2016, 6:10 AM
Prasanth Ravichandran
Prasanth Ravichandran - avatar
2 Answers
0
What do you mean by why? I am not sure if you know this already. from itertools import accumulate, takewhile nums = list(accumulate(range(8))) print(nums) _________________ [0, 1, 3, 6, 10, 15, 21, 28] range function makes [0,1,2,3,4,5,6,7] and accumulate function does [0,0+1,0+1+2,0+1+2+3,0+1+2+3+4,and so on] #it adds previous list items this equals to [0,1,3,6,10,15, etc]
3rd Jan 2018, 7:29 AM
Yololab
Yololab - avatar
0
The other way to do this is to use def function(i think you know this already)
3rd Jan 2018, 7:32 AM
Yololab
Yololab - avatar