Home > Software engineering >  How to create new dictionaries from nested dictionary?
How to create new dictionaries from nested dictionary?

Time:05-15

I have a nested dictionary. How do I access them and get the result as follows:

dict_lbl = {
    "lbl1":{"name":"label1","item1":"Accounts",   "item2":"kannagu",    "shortcut":"F1","printitem":"You clicked label1"},
    "lbl2":{"name":"label2","item1":"Inventory",  "item2":"Saragu",     "shortcut":"F2","printitem":"You clicked label2"},
    "lbl3":{"name":"label3","item1":"Manufacture","item2":"Thayarippu", "shortcut":"F3","printitem":"You clicked label3"},
    "lbl4":{"name":"label4","item1":"PayRoll",    "item2":"Sambalam",   "shortcut":"F4","printitem":"You clicked label4"}
     }

Need Result as Follows:

dict_name = {"lbl1":"label1","lbl2":"label2","lbl3":"label3","lbl4":"label4"}
dict_item1 ={"lbl1":"Accounts","lbl2":"Inventory","lbl3":"Manufacture","lbl4":"PayRoll"}
dict_item2 ={"lbl1":"kannagu","lbl2":"saragu","lbl3":"Thayaripu","lbl4":"Sambalam"}
dict_shortcut ={"lbl1":"F1","lbl2":"F2","lbl3":"F3","lbl4":"F4"}
dict_printitem = {"lbl1":"You clicked label1","lbl2":"You clicked label1","lbl3":"You clicked label1","lbl4":"You clicked label1"}

CodePudding user response:

dict_name = {k: v['name'] for k, v in dict_lbl.items()}
dict_item1 = {k: v['item1'] for k, v in dict_lbl.items()}
# etc...

CodePudding user response:

You can do this in a single nested comprehension to produce a dict of dicts:

>>> dict_lbl = {
...     "lbl1":{"name":"label1","item1":"Accounts",   "item2":"kannagu",    "shortcut":"F1","printitem":"You clicked label1"},
...     "lbl2":{"name":"label2","item1":"Inventory",  "item2":"Saragu",     "shortcut":"F2","printitem":"You clicked label2"},
...     "lbl3":{"name":"label3","item1":"Manufacture","item2":"Thayarippu", "shortcut":"F3","printitem":"You clicked label3"},
...     "lbl4":{"name":"label4","item1":"PayRoll",    "item2":"Sambalam",   "shortcut":"F4","printitem":"You clicked label4"}
... }
>>> dict_key = {k: {n: d[k] for n, d in dict_lbl.items()} for k in {k for d in dict_lbl.values() for k in d}}
>>> import pprint
>>> pprint.pprint(dict_key)
{'item1': {'lbl1': 'Accounts',
           'lbl2': 'Inventory',
           'lbl3': 'Manufacture',
           'lbl4': 'PayRoll'},
 'item2': {'lbl1': 'kannagu',
           'lbl2': 'Saragu',
           'lbl3': 'Thayarippu',
           'lbl4': 'Sambalam'},
 'name': {'lbl1': 'label1',
          'lbl2': 'label2',
          'lbl3': 'label3',
          'lbl4': 'label4'},
 'printitem': {'lbl1': 'You clicked label1',
               'lbl2': 'You clicked label2',
               'lbl3': 'You clicked label3',
               'lbl4': 'You clicked label4'},
 'shortcut': {'lbl1': 'F1', 'lbl2': 'F2', 'lbl3': 'F3', 'lbl4': 'F4'}}

You can then do dict_name = dict_key['name'] etc if you desire, but you may actually find it easier to just keep this in a single nested dict.

CodePudding user response:

Use dictionary comprehension:

dict_names = {k: v['name'] for k, v in dict_lbl.items()}
dict_item1 = {k: v['item1'] for k, v in dict_lbl.items()}
dict_item2 = {k: v['item2'] for k, v in dict_lbl.items()}

# etc.
  • Related