Reputation: 1538
I have two lists:
ramp_versions=['RAMPS_1_6','RAMPS_10','RAMPS_11','RAMPS_7','RAMPS_8_9']
columns=['Version','Date','Total_Ramps','Virtual_Ramps','Real_Ramps','Real_Ramps_With_All_Measurements','Real_Ramps_Unsurveyable','Real_Ramps_Partial_With_999','Real_Ramps_Partial_WO_999','Real_Ramps_Partial_W_RAMP_WIDTH_<36_or_DWS_BAD']
I want to create a nested dictionary where each ramp_version is a key and the values are a dictionary where each value in the columns is a key
So this is what I am after:
{'RAMPs_1_6': {'Version': "value",'Date': "value", 'Total_Ramps': "value", etc..},'RAMPS_10': {'Version': "value",'Date': "value", 'Total_Ramps': "value", etc..}
Upvotes: 0
Views: 2357
Reputation: 11
Simply run a loop for keys and set values for that in an empty defined dictionary
see the following example:
ramp_versions=['RAMPS_1_6','RAMPS_10','RAMPS_11','RAMPS_7','RAMPS_8_9']
columns=['Version','Date','Total_Ramps','Virtual_Ramps','Real_Ramps','Real_Ramps_With_All_Measurements','Real_Ramps_Unsurveyable','Real_Ramps_Partial_With_999','Real_Ramps_Partial_WO_999','Real_Ramps_Partial_W_RAMP_WIDTH_<36_or_DWS_BAD']
my_dict = {}
i=0
for ramp in ramp_versions:
my_dict[ramp] = columns[i]
i+=1
print(my_dict)
# print(my_dict.keys())
# print(my_dict.values())
Upvotes: 0
Reputation: 850
Another version:
nest = {}
for r in ramp_versions:
nest[r] = dict(zip(columns, ['val' for x in columns]))
Upvotes: 0
Reputation: 519
Use dict comprehension:
{ver: {col: "value" for col in columns} for ver in ramp_versions}
Output:
{'RAMPS_1_6': {'Version': 'value',
'Date': 'value',
'Total_Ramps': 'value',
'Virtual_Ramps': 'value',
'Real_Ramps': 'value',
'Real_Ramps_With_All_Measurements': 'value',
'Real_Ramps_Unsurveyable': 'value',
'Real_Ramps_Partial_With_999': 'value',
'Real_Ramps_Partial_WO_999': 'value',
'Real_Ramps_Partial_W_RAMP_WIDTH_<36_or_DWS_BAD': 'value'},
'RAMPS_10': {'Version': 'value',
'Date': 'value',
'Total_Ramps': 'value',
'Virtual_Ramps': 'value',
'Real_Ramps': 'value',
'Real_Ramps_With_All_Measurements': 'value',
'Real_Ramps_Unsurveyable': 'value',
'Real_Ramps_Partial_With_999': 'value',
'Real_Ramps_Partial_WO_999': 'value',
'Real_Ramps_Partial_W_RAMP_WIDTH_<36_or_DWS_BAD': 'value'},
'RAMPS_11': {'Version': 'value',
'Date': 'value',
'Total_Ramps': 'value',
'Virtual_Ramps': 'value',
'Real_Ramps': 'value',
'Real_Ramps_With_All_Measurements': 'value',
'Real_Ramps_Unsurveyable': 'value',
'Real_Ramps_Partial_With_999': 'value',
'Real_Ramps_Partial_WO_999': 'value',
'Real_Ramps_Partial_W_RAMP_WIDTH_<36_or_DWS_BAD': 'value'},
'RAMPS_7': {'Version': 'value',
'Date': 'value',
'Total_Ramps': 'value',
'Virtual_Ramps': 'value',
'Real_Ramps': 'value',
'Real_Ramps_With_All_Measurements': 'value',
'Real_Ramps_Unsurveyable': 'value',
'Real_Ramps_Partial_With_999': 'value',
'Real_Ramps_Partial_WO_999': 'value',
'Real_Ramps_Partial_W_RAMP_WIDTH_<36_or_DWS_BAD': 'value'},
'RAMPS_8_9': {'Version': 'value',
'Date': 'value',
'Total_Ramps': 'value',
'Virtual_Ramps': 'value',
'Real_Ramps': 'value',
'Real_Ramps_With_All_Measurements': 'value',
'Real_Ramps_Unsurveyable': 'value',
'Real_Ramps_Partial_With_999': 'value',
'Real_Ramps_Partial_WO_999': 'value',
'Real_Ramps_Partial_W_RAMP_WIDTH_<36_or_DWS_BAD': 'value'}}
Upvotes: 3