Given the following list:
a=[0,1,2,3]
I would like to create a new list, b, which consists of elements for which the current and next value of a are summed. It will contain 1 element less than a.
Like this: [1,3,5]
(from 0 + 1, 1 + 2 and 2 + 3)
Here is what I tried:
b=[] for i in a: b.append(a[i+1]-a[i]) b
The problem is that I keep getting this error:
IndexError: list index out of range
I am sure this is happening, because by the time I get the last element (3), I canβt add it to anything, because it goes beyond its value (after the value there is no 3 value to add). So I need to say that the code stops at 2, but still refers to 3 for calculation.
Thanks in advance!
source share