Skip to content
Advertisement

How to subtract values from a list with values from the same list?

I have the following list:

91747, 280820, 334845, 531380, 585594, 657296, 711726, ...

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

189073, 54025, ...

Advertisement

Answer

You can do this :

# the source list
mylist = [91747, 280820, 334845, 531380, 585594, 657296, 711726]

# the new output list using list comprehension
result = [x - y for x, y in zip(mylist[1:], mylist[:len(mylist)-1])]

# printing the result
print(result)

Output :

[189073, 54025, 196535, 54214, 71702, 54430]
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement