Sum of consecutive pairs in a list including a sum of the last element with the first
List2 = [List1[i] + List1[(i+1)%len(List1)] for i in range (len(List1))]
[List1[i] + List1[(i+1) % len(List1)] for i in range(len(List1))]
or
[sum(tup) for tup in zip(List1, List1[1:] + [List1[0]])]
or
[x + y for x, y in zip(List1, List1[1:] + [List1[0]])]
Because of the i+1
, the index goes out of range
List1 = [1,3,5,6,8,7]
List2 = [List1[i-1] + List1[i] for i in range (len(List1))]
print (List2)
This way kinda works
Result:
[8, 4, 8, 11, 14, 15]