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 list of dictionaries

I have below list of dictionary:

 [{'first': '0', 'last': 'hg', 'pay': '0', 'hossain': '{}'},
 {'first': '0', 'last': 'hg', 'pay': '195', 'hossain': '{}'},
 {'first': '0', 'last': 'hg', 'pay': '1', 'hossain': '{}'},
 {'first': '0', 'last': 'hg', 'pay': '344', 'hossain': '{}'},
 {'first': '0', 'last': 'hg', 'pay': '4', 'hossain': '{}'},
 {'first': '0', 'last': 'hg', 'pay': '6', 'hossain': '{}'},
 {'first': '0', 'last': 'hg', 'pay': '5', 'hossain': '{}'},
 {'first': '0', 'last': 'hg', 'pay': '7', 'hossain': '{}'},
 {'first': '0', 'last': 'hg', 'pay': '8', 'hossain': '{}'},
 {'first': '0', 'last': 'hg', 'pay': '9', 'hossain': '{}'}]

and I want convert it to a nested dictionary with key’s ‘pay’:

 {'0':{'first': '0', 'last': 'hg',  'hossain': '{}'},
 '195':{'first': '0', 'last': 'hg', 'hossain': '{}'},
 '1':{'first': '0', 'last': 'hg',   'hossain': '{}'},
 '344':{'first': '0', 'last': 'hg', 'hossain': '{}'},
 '4':{'first': '0', 'last': 'hg',   'hossain': '{}'},
 '6':{'first': '0', 'last': 'hg',   'hossain': '{}'},
 '5':{'first': '0', 'last': 'hg',   'hossain': '{}'},
 '7':{'first': '0', 'last': 'hg',   'hossain': '{}'},
 '8':{'first': '0', 'last': 'hg',   'hossain': '{}'},
 '9':{'first': '0', 'last': 'hg',   'hossain': '{}'}}

I need a fast way to convert it
thank you for help

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 :

You have below methods for doing this:

Method 1: Using for loop


data =  [{'first': '0', 'last': 'hg', 'pay': '0', 'hossain': '{}'},
 {'first': '0', 'last': 'hg', 'pay': '195', 'hossain': '{}'},
 {'first': '0', 'last': 'hg', 'pay': '1', 'hossain': '{}'},
 {'first': '0', 'last': 'hg', 'pay': '344', 'hossain': '{}'},
 {'first': '0', 'last': 'hg', 'pay': '4', 'hossain': '{}'},
 {'first': '0', 'last': 'hg', 'pay': '6', 'hossain': '{}'},
 {'first': '0', 'last': 'hg', 'pay': '5', 'hossain': '{}'},
 {'first': '0', 'last': 'hg', 'pay': '7', 'hossain': '{}'},
 {'first': '0', 'last': 'hg', 'pay': '8', 'hossain': '{}'},
 {'first': '0', 'last': 'hg', 'pay': '9', 'hossain': '{}'}]

dictionary_of_pays = {}
 
# for loop to convert a list of dict
# to dict of list
for item in data:
    key = item['pay']
    item.pop('pay')
    dictionary_of_pays[key] = item
 
# display
print(dictionary_of_pays)

output:

 {'0':{'first': '0', 'last': 'hg',  'hossain': '{}'},
 '195':{'first': '0', 'last': 'hg', 'hossain': '{}'},
 '1':{'first': '0', 'last': 'hg',   'hossain': '{}'},
 '344':{'first': '0', 'last': 'hg', 'hossain': '{}'},
 '4':{'first': '0', 'last': 'hg',   'hossain': '{}'},
 '6':{'first': '0', 'last': 'hg',   'hossain': '{}'},
 '5':{'first': '0', 'last': 'hg',   'hossain': '{}'},
 '7':{'first': '0', 'last': 'hg',   'hossain': '{}'},
 '8':{'first': '0', 'last': 'hg',   'hossain': '{}'},
 '9':{'first': '0', 'last': 'hg',   'hossain': '{}'}}

Method 2: Using dictionary comprehension

dictionary_of_pays = {item['pay']:{k:item[k] for k in item if k != 'pay'} for item in data}


print(dictionary_of_pays)

output:

 {'0':{'first': '0', 'last': 'hg',  'hossain': '{}'},
 '195':{'first': '0', 'last': 'hg', 'hossain': '{}'},
 '1':{'first': '0', 'last': 'hg',   'hossain': '{}'},
 '344':{'first': '0', 'last': 'hg', 'hossain': '{}'},
 '4':{'first': '0', 'last': 'hg',   'hossain': '{}'},
 '6':{'first': '0', 'last': 'hg',   'hossain': '{}'},
 '5':{'first': '0', 'last': 'hg',   'hossain': '{}'},
 '7':{'first': '0', 'last': 'hg',   'hossain': '{}'},
 '8':{'first': '0', 'last': 'hg',   'hossain': '{}'},
 '9':{'first': '0', 'last': 'hg',   'hossain': '{}'}}
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