Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how can I create a nested dictionary with use a same key in list of dictionaries

I have this dictionary:

data=[{'first': '0', 'last': 'hg01', 'pay': 0},
 {'first': '0', 'last': 'hg75', 'pay': 15},
 {'first': '0', 'last': 'hg0', 'pay': 1},
 {'first': '0', 'last': 'hg9', 'pay': 13},
 {'first': '0', 'last': 'hg0', 'pay': 0},
 {'first': '0', 'last': 'hg', 'pay': 13},
 {'first': '0', 'last': 'hg76', 'pay': 0},
 {'first': '0', 'last': 'hg1', 'pay': 9},
 {'first': '0', 'last': 'hg81', 'pay': 13},
 {'first': '0', 'last': 'hg12', 'pay': 1},
 {'first': '0', 'last': 'hg03', 'pay': 15},
 {'first': '0', 'last': 'hg11', 'pay': 9},
 {'first': '0', 'last': 'hg1', 'pay': 1}]

and I want convert to this dictionary with dictionary or list comprehension:

output={0: {'hg76':{'first': '0'},'hg0':{'first': '0'},'hg01':{'first': '0'}},
        15: {'hg75':{'first': '0'},'hg03':{'first': '0'}},
        1: {'hg1':{'first': '0'},'hg12':{'first': '0'},'hg0':{'first': '0'}},
        13: {'hg81':{'first': '0'},'hg':{'first': '0'},'hg9':{'first': '0'}},
        9: {'hg1':{'first': '0'},'hg11':{'first': '0'}}}

I don’t have any ideas for this problem

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This can be done with a nested comprehension, without any packages:

data=[{'first': '0', 'last': 'hg01', 'pay': 0},
 {'first': '0', 'last': 'hg75', 'pay': 15},
 {'first': '0', 'last': 'hg0', 'pay': 1},
 {'first': '0', 'last': 'hg9', 'pay': 13},
 {'first': '0', 'last': 'hg0', 'pay': 0},
 {'first': '0', 'last': 'hg', 'pay': 13},
 {'first': '0', 'last': 'hg76', 'pay': 0},
 {'first': '0', 'last': 'hg1', 'pay': 9},
 {'first': '0', 'last': 'hg81', 'pay': 13},
 {'first': '0', 'last': 'hg12', 'pay': 1},
 {'first': '0', 'last': 'hg03', 'pay': 15},
 {'first': '0', 'last': 'hg11', 'pay': 9},
 {'first': '0', 'last': 'hg1', 'pay': 1}]

unique_pay_vals = {d['pay'] for d in data}
new_dict = {p: {d['last']: {'first': d['first']} for d in data if d['pay'] == p} 
            for p in unique_pay_vals}

print(new_dict)
# {0: {'hg01': {'first': '0'}, 'hg0': {'first': '0'}, 'hg76': {'first': '0'}}}
# {1: {'hg0': {'first': '0'}, 'hg12': {'first': '0'}, 'hg1': {'first': '0'}}}
# {9: {'hg1': {'first': '0'}, 'hg11': {'first': '0'}}}
# {13: {'hg9': {'first': '0'}, 'hg': {'first': '0'}, 'hg81': {'first': '0'}}}
# {15: {'hg75': {'first': '0'}, 'hg03': {'first': '0'}}}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading