Is there a way to write the following code in a more concise (or more Pythonish) way?
[ counters[i]-counters[i-1] for i in range(1, len(counters)) ]
Is there a way to write the following code in a more concise (or more Pythonish) way?
[ counters[i]-counters[i-1] for i in range(1, len(counters)) ]
You can do that using zip
:
[b-a for a,b in zip(counters,counters[1:])]
counters = [1,6,3,8,4,6]
print [b-a for a,b in zip(counters,counters[1:])]
[OUTPUT]
[5, -3, 5, -4, 2]