python - Merge dictionaries by key with dictionary names as subkeys -


i looking short , pythonic way merge 2 (three, n) dictionaries key. there couple of questions on merging or appending dictionaries, here looking appending value value when keys equal. dictionaries have same set of keys.

dic0 = {'amsterdam': 0, 'berlin': 5, 'london': 7} dic1 = {'amsterdam': 1, 'berlin': 7, 'london': 8} 

desired output:

combined_dict = {'amsterdam': {'dic0': 0, 'dic1':1}, 'berlin': {'dic0': 5, 'dic1':7}, 'london': {'dic0': 7, 'dic1':8}} 

i achieve with

combined_dict = {} key in dic0.keys():     combined_dict[key] = {}     combined_dict[key]['dic0'] = dic0[key]     combined_dict[key]['dic1'] = dic1[key] 

but seems far easy extend. first step, failed name of dictionaries.

this work list of dictionary names, example dicts = ['dic0', 'dic1']:

combo = {k: {i: eval(i)[k] in dicts} k in eval(dicts[0]).keys()}  

Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -