Bob Bob - 1 year ago 78
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 Source


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?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download