Given the following list
a = [0, 1, 2, 3]
I'd like to create a new list b, which consists of elements for which the current and next value are summed. It will contain 1 less element than a.
Like this:
b = [1, 3, 5]
(from 0+1, 1+2, and 2+3)
Here's what I've tried:
b = []
for i in a:
b.append(a[i + 1] - a[i])
b
The trouble is I keep getting this error:
IndexError: list index out of range
I'm pretty sure it occurs because, by the time I get the last element of a (3), I can't add it to anything because doing so goes outside of the value of it (there is no value after 3 to add). So I need to tell the code to stop at 2 while still referring to 3 for the calculation.