Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

189073, 54025, ...

>Solution :

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]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading