Home > Software engineering >  how can I create a nested dictionary with use a same key in list of dictionaries
how can I create a nested dictionary with use a same key in list of dictionaries

Time:11-09

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

CodePudding user response:

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'}}}

CodePudding user response:

Try itertools.groupby and dict comprehension

from itertools import groupby
{k:{v1['last']: {'first': v1['first']} for v1 in v} for k,v in groupby(sorted(data, key=lambda x: x["pay"]), key=lambda x: x["pay"])}

#output
{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'}}}

CodePudding user response:

I would use dict.setdefault here:

out = {}
for d in data:
    out.setdefault(d['pay'], {})[d['last']] = {'first': d['first']}

output:

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