# How to Create a Dictionary from Values Inside Double For Loop in Python?

Solution for How to Create a Dictionary from Values Inside Double For Loop in Python?
is Given Below:

How would I create a dictionary with the titles ‘Latitude’, ‘Longitude’, ‘DST_R’, and ‘Kp_R’ and save dst_R and kp_R for each iteration?

Below is the code I currently have and made sure it was printing all the values I want correctly but I really want to save the latitude (-90 to 90), longitude (0 to 359), dst_R, and kp_R in a dictionary.

For reference df_dst has the following format:

and df_kp has the following format:

``````for i in np.arange(0,360): # looping through longitude
for j in np.arange(-90,91): # looping through latitude

# DST Rigidity
dst_R = df_dst.query('lat == ' + str(j) + 'and lon == ' + str(i)).R

# Kp Rigidity
west_subset = West.query('times>= "2017-09-10 17:00:00" and times < "2017-09-10 19:05:00"')

for t in range(len(west_subset.times)):
current_time = west_subset.times.iloc[t]
current_hour = current_time.hour
if np.logical_and(current_hour >= 0, current_hour < 3):
kp_tmp = kp.query('date == "' + datetime.strftime(west_subset.times.iloc[t], '%Y-%m-%d 00:00:00')+'"').kp.values[0]
elif np.logical_and(current_hour >= 3, current_hour < 6):
kp_tmp = kp.query('date == "' + datetime.strftime(west_subset.times.iloc[t], '%Y-%m-%d 03:00:00')+'"').kp.values[0]
elif np.logical_and(current_hour >= 6, current_hour < 9):
kp_tmp = kp.query('date == "' + datetime.strftime(west_subset.times.iloc[t], '%Y-%m-%d 06:00:00')+'"').kp.values[0]
elif np.logical_and(current_hour >= 9, current_hour < 12):
kp_tmp = kp.query('date == "' + datetime.strftime(west_subset.times.iloc[t], '%Y-%m-%d 09:00:00')+'"').kp.values[0]
elif np.logical_and(current_hour >= 12, current_hour < 15):
kp_tmp = kp.query('date == "' + datetime.strftime(west_subset.times.iloc[t], '%Y-%m-%d 12:00:00')+'"').kp.values[0]
elif np.logical_and(current_hour >= 15, current_hour < 18):
kp_tmp = kp.query('date == "' + datetime.strftime(west_subset.times.iloc[t], '%Y-%m-%d 15:00:00')+'"').kp.values[0]
elif np.logical_and(current_hour >= 18, current_hour < 21):
kp_tmp = kp.query('date == "' + datetime.strftime(west_subset.times.iloc[t], '%Y-%m-%d 18:00:00')+'"').kp.values[0]
else:
kp_tmp = kp.query('date == "' + datetime.strftime(west_subset.times.iloc[t], '%Y-%m-%d 21:00:00')+'"').kp.values[0]

if kp_tmp < 5:
kp_R = df_kp.query('LAT == ' + str(j) + 'and LON == ' + str(i)).R_quiet
else:
kp_R = df_kp.query('LAT == ' + str(j) + 'and LON == ' + str(i)).R_storm
``````

Ultimately I want the dictionary to look something like:

``````Latitude  Longitude  DST_R                 Kp_R
-90       0          -0.3013268844290973   0.03
-89       0          -0.29102307011436673  0.03
-88       0          -0.27900238806939714  0.05
-87       0          -0.2651162151965792   0.07
...
``````

What I had tried previously isn’t working because I’m not able to iterate through `dst_R` by doing `dst_R.item()[i]` and same for kp_R. So I don’t know how to save these values without replacing them every time it runs through the loop. I appreciate any and all advice!