I need the answer, I don't know why this output, and I need the explication of itertools | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 9

I need the answer, I don't know why this output, and I need the explication of itertools

https://code.sololearn.com/c7dJxwWk5RnT/?ref=app

16th Sep 2021, 12:27 AM
CGO!
CGO! - avatar
5 Answers
+ 9
The accumulate function in the itertools module, by default, will take in an iterable and returns an iterable that keeps a running sum of its items. The original range converted to a list would be; [0, 1, 2, 3, 4, 5, 6, 7] Where the accumulated list returned is; [0, 1, 3, 6, 10, 15, 21, 28] Notice if you start at the 1st element and then add the values of the previous elements that the sum of those values results in the current elements value. The element at index 0 is 0 The element at index 1 is equal to the element at index 0 + the element at index 1. So it is equal to 1 (0+1) The element at index 2 is equal to the sum of elements at index 0 and 1 + the element at index 2. So it is equal to 3 ((0+1)+2) And so on .... https://docs.python.org/3/library/itertools.html https://docs.python.org/3/library/itertools.html#itertools.accumulate
16th Sep 2021, 1:55 AM
ChaoticDawg
ChaoticDawg - avatar
16th Sep 2021, 12:48 AM
CGO!
CGO! - avatar
+ 5
16th Sep 2021, 12:34 PM
CGO!
CGO! - avatar
+ 4
Caleb, that's very easy, you are using function accumulate, which takes an iterable as argument, that function sums all the numbers given in its argument. In your program, you are passing to accumulate an iterable range(8) --> 0,1,2,3,4,5,6,7 So: 0+1 = |1|+2 = |3|+3 = |6|+4 = |10|+5 = |15|+6 = |21|+7 = |28| You're also using list() which converts its argument in a list, So the output is: [1, 3, 6, 10, 15, 21, 28]
16th Sep 2021, 7:26 AM
Hacker-KR4636
Hacker-KR4636 - avatar
+ 3
The function results are x0, x0+x1, x0+x1+x2, …
16th Sep 2021, 3:18 AM
JaScript
JaScript - avatar