Using zip
and list comprehension
:
>>> [ a-b for a,b in zip(mylist[1:], mylist[:-1]) ]
=> [4, 10, 15, 30]
Values#driver
IN : mylist = [1,5,15,30,60]
In case you want the old style to go through the list using it index
:
>>> [ mylist[i+1]-mylist[i] for i in range(len(mylist)-1) ]
=> [4, 10, 15, 30]
Although the use is zip
better than moving it index
, because you do not need to generate a list for range
, as in the case of python2
more Pythonic .
EDIT: The method zip
could also be simplezip(mylist[1:], mylist[:])