# How access different elements of a list for arithmetic operations

I am trying to calculate the distance between coordinates (x, y). However I don’t know which loop I should use.
My idea is:

``````import math as m
station=[]

x = [777610.744,777694.822,778074.698,778546.91,778980.244,779348.78]
y = [8433327.49,8433273.408,8432998.353,8432656.44,8432342.677,8432075.832]

station = mt.sqrt((x - x)**2+(y - y)**2)
station = mt.sqrt((x - x)**2+(y - y)**2)
station = mt.sqrt((x - x)**2+(y - y)**2)
station = mt.sqrt((x - x)**2+(y - y)**2)
station = mt.sqrt((x - x)**2+(y - y)**2)

print(station)
``````

Which is not possible …
I’ve tried something with zip function, but I don’t know how to access an element in the next iterative loop:

``````import math as m
station=[]

x = [777610.744,777694.822,778074.698,778546.91,778980.244,779348.78]
y = [8433327.49,8433273.408,8432998.353,8432656.44,8432342.677,8432075.832]

for i, j in zip(x, y):
?????
station.append(result)
``````

Just playing around and came up with the following…

``````>>> u = [(x[a + 1] - x[a]) ** 2 for a in range(len(x) - 1)]
>>> v = [(y[a + 1] - y[a]) ** 2 for a in range(len(y) - 1)]
>>> station = [round(sqrt(i + j), 3) for i, j in zip(u, v)]
>>> station
[99.97, 469.0, 583.0, 535.001, 455.0]
>>>
``````
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.