How to get the difference between consecutive numbers in a list with Python

2 Answers

0 votes
lst = [3, 7, 9, 10, 17, 28]

result = [item - lst[index - 1] for index, item in enumerate(lst)][1:]

print(result) 



'''
run:

[4, 2, 1, 7, 11]

'''

 



answered Jul 9, 2022 by avibootz
0 votes
lst = [3, 7, 9, 10, 17, 28]

result = [y - x for x, y in zip(lst[:-1], lst[1:])]

print(result) 



'''
run:

[4, 2, 1, 7, 11]

'''

 



answered Jul 9, 2022 by avibootz

Related questions

...