try:
new_dic['Name_1'] = source_1['key_1']['key_2']['key_3']
except:
new_dic['Name_1'] = None
try:
new_dic['Name_2'] = source_2['key_1']['key_2']
except:
new_dic['Name_2'] = None
try:
new_dic['Name_3'] = source_1['key_1']['key_2']['key_3']['key_4']
except:
new_dic['Name_3'] = None
...
def dict_get_recursive(source, path):
current_dict = source
for path_idx, path_key in enumerate(path):
if path_idx == len(path)-1:
return current_dict.get(path_key, None)
current_dict = current_dict.get(path_key, None)
if current_dict is None:
return None
new_dic['Name_1'] = dict_get_recursive(source_1,['key_1','key_2','key_3'])
new_dic['Name_2'] = dict_get_recursive(source_2,['key_1','key_2'])
new_dic['Name_3'] = dict_get_recursive(source_1,['key_1','key_2','key_3','key_4'])