I have the following list:
JavaScript
x
2
1
91747, 280820, 334845, 531380, 585594, 657296, 711726,
2
I want to subtract the second value with the first value, the third with the second, and so on. The results I want to transfer into a new list.
The output should be
JavaScript
1
2
1
189073, 54025,
2
Advertisement
Answer
You can do this :
JavaScript
1
9
1
# the source list
2
mylist = [91747, 280820, 334845, 531380, 585594, 657296, 711726]
3
4
# the new output list using list comprehension
5
result = [x - y for x, y in zip(mylist[1:], mylist[:len(mylist)-1])]
6
7
# printing the result
8
print(result)
9
Output :
JavaScript
1
2
1
[189073, 54025, 196535, 54214, 71702, 54430]
2