I'm attempting to iterate through a list of dictionaries and extract values based on the key and the dictionary index.
My goal is to assign the values from my first dictionary to the variables x1/y1, then move on and do the same with my second dictionary to variables (x2, y2), I will then perform a calculation. Once the calculation has been performed, I'd like to do the same with dictionary 2/3 and so on until I have calculated all of the dictionaries in the list.
I'm stuck because I cannot reference the dictionary index. Currently my code only stores values in lon1/lat1.
lst = [{'lat': 1, 'Time': 1, 'lon': 1},
{'lat': 2, 'Time': 2, 'lon': 2},
{'lat': 3, 'Time': 3, 'lon': 3}]
x1 = ()
x2 = ()
y1 = ()
y2 = ()
for k, v in [(k, v) for x in lst for (k, v) in x.items()]:
if k == 'lon'
x1 = v
elif k == 'lat':
y1 = v
elif k == 'lat':
x2 = v
elif k == 'lon':
y2 = v
Edit: Removed unnecessary code and attempted explain my goal more concisely. I elected to keep the unnecessary variables because I’m required to use a canned formula with those variables for the calculation.