Is there any function to put dictionary inside dictionary?

56 views Asked by At

First of all, I want to open the dictionary and change the dictionary key. I have tried using zip but it is not working as I want.

This is the data that I have,

data= [{'id': 'abc001',
       'demo': [{'Answer': '1', 'QID': 'AB001'},
                {'Answer': '6', 'QID': 'AB023'},
                {'Answer': '3', 'QID': 'AB004'},
                {'Answer': '3', 'QID': 'AB006'}]},
      {'id': 'abc002',
       'demo': [{'Answer': '1', 'QID': 'AB001'},
                {'Answer': '6', 'QID': 'AB002'},
                {'Answer': '3', 'QID': 'AB003'},
                {'Answer': '3', 'QID': 'AB006'}]},
      {'id': 'abc003',
       'demo': [{'Answer': '1', 'QID': 'AB001'},
                {'Answer': '6', 'QID': 'AB002'},
                {'Answer': '3', 'QID': 'AB004'},
                {'Answer': '3', 'QID': 'AB005'}]}]

I want to change the QID to another name BUT there are responses that have this QID and then another response has other QID as in data

This is the name that need to change for the QID:

QID 
AB001-gender
AB002-edu
AB003-qual
AB004-area
AB005-sal
AB006-living
AB023-job

Hence, this is the output that I need : The dictionary only inside the demo.

Output=[{'id': 'abc001'
        'demo':{'gender':'1',
               'job':'6',
               'area':'3',
               'study':'3'}},
       {'id': 'abc002'
        'demo':{'gender':'1',
               'edu':'6',
               'qual':'3',
               'living':'3'}},
       {'id': 'abc003'
        'demo':{'gender':'1',
               'edu':'6',
               'area':'3',
               'sal':'3'}}]
3

There are 3 answers

0
marcos On BEST ANSWER

You can just do this:

mod_data = []
for demo in data:
    mod_demo = {'id': demo['id'], 'demo': {}}
    for answer in demo['demo']:
        key = mapping[answer['QID']]
        mod_demo['demo'][key] = answer['Answer']

    mod_data.append(mod_demo)

print(mod_data)
>>> [
    {'id': 'abc001', 'demo': {'gender': '1', 'job': '6', 'area': '3', 'living': '3'}},
    {'id': 'abc002', 'demo': {'gender': '1', 'edu': '6', 'qual': '3', 'living': '3'}},
    {'id': 'abc003', 'demo': {'gender': '1', 'edu': '6', 'area': '3', 'sal': '3'}}
]
0
FrahChan04 On

I have tried using this one.

data= [{'id': 'abc001',
       'demo': [{'Answer': '1', 'QID': 'AB001'},
                {'Answer': '6', 'QID': 'AB023'},
                {'Answer': '3', 'QID': 'AB004'},
                {'Answer': '3', 'QID': 'AB006'}]},
      {'id': 'abc002',
       'demo': [{'Answer': '1', 'QID': 'AB001'},
                {'Answer': '6', 'QID': 'AB002'},
                {'Answer': '3', 'QID': 'AB003'},
                {'Answer': '3', 'QID': 'AB006'}]},
      {'id': 'abc003',
       'demo': [{'Answer': '1', 'QID': 'AB001'},
                {'Answer': '6', 'QID': 'AB002'},
                {'Answer': '3', 'QID': 'AB004'},
                {'Answer': '3', 'QID': 'AB005'}]}]

id_to_key = {
  'AB001': 'gender',
  'AB002': 'edu',
  'AB003': 'qual',
  'AB004': 'area',
  'AB005': 'sal',
  'AB006': 'living',
  'AB023': 'job'
}
import pprint

result = []
for item in data:
  d = {'id': item['id']}
  dd = {}
  for question in item['demo']:
    dd[id_to_key[question['QID']]] = question['Answer']
  d['demo'] = dd
  result.append(d)
pprint.pprint(result)
0
Joran Beasley On

I didnt bother testing it ... so it might not work out of the box

s= """AB001-gender
AB002-edu
AB003-qual
AB004-area
AB005-sal
AB006-living
AB023-job"""
# create a lookup table
id2name = dict(line.split("-",1) for line in s.splitlines())



data= [{'id': 'abc001',
       'demo': [{'Answer': '1', 'QID': 'AB001'},
                {'Answer': '6', 'QID': 'AB023'},
                {'Answer': '3', 'QID': 'AB004'},
                {'Answer': '3', 'QID': 'AB006'}]},
      {'id': 'abc002',
       'demo': [{'Answer': '1', 'QID': 'AB001'},
                {'Answer': '6', 'QID': 'AB002'},
                {'Answer': '3', 'QID': 'AB003'},
                {'Answer': '3', 'QID': 'AB006'}]},
      {'id': 'abc003',
       'demo': [{'Answer': '1', 'QID': 'AB001'},
                {'Answer': '6', 'QID': 'AB002'},
                {'Answer': '3', 'QID': 'AB004'},
                {'Answer': '3', 'QID': 'AB005'}]}]
def lookup_dict_data(d):
    # look up the id in our other dict we created earlier
    return id2name.get(d['QID'],d['QID']), d['Answer'])

for adict in data: # edit existing dict i guess
    adict['demo'] = dict(lookup_dict_data(d) for d in adict['demo'])