Bob Bob - 5 months ago 9
Python Question

pairwise traversal of a list or tuple

a = [5, 66, 7, 8, 9, ...]


Is it possible to make an iteration instead of writing like this?

a[1] - a[0]

a[2] - a[1]

a[3] - a[2]

a[4] - a[3]


...

Thank you!

Answer

Sure.

for i in range(1, len(a)):
    print a[i] - a[i-1]

I fail to see what the real problem is here. Have you read the python tutorial?

Comments