简体   繁体   中英

How to iterate through list of dictionary, extract values and fill in another data dictionary in python

I have a list of dictionary as below.

[
    {'name':['mallesh'],'email':['m@gmail.com']},
    {'name':['bhavik'],'ssn':['1000011']},
    {'name':['jagarini'],'email':['m@gmail.com'],'phone':['111111']},
    {'name':['mallesh'],'email':['m@gmail.com'],'phone':['1234556'],'ssn':['10000012']}
]

I would like to extract the information from these dictionary based on keys, hold on its information in another dictionary as.

xml_master_dict={'name':[],'email':[],'phone':[],'ssn':[]}

Here xml_master_dict should be filled in with the respective key information as below.

In a fist dictionary we have this:

{'name':['mallesh'],'email':['m@gmail.com']}

In xml_master_dict name and email keys only will be updated with the current value, if any of key is not existed in the dictionary it should be filled in with None. in this case phone and ssn will be None

Here is an expected output:

{
 'name':['mallesh','bhavik','jagarini','mallesh'],
 'email':['m@gmail.com',None,'m@gmail.com','m@gmail.com'],
 'phone':[None,None,'111111','1234556'],
 'ssn':[None,'1000011',None,'10000012'],
}
pd.DataFrame({
 'name':['mallesh','bhavik','jagarini','mallesh'],
 'email':['m@gmail.com',None,'m@gmail.com','m@gmail.com'],
 'phone':[None,None,'111111','1234556'],
 'ssn':[None,'1000011',None,'10000012'],
})

在此处输入图像描述

You can define a function to get the first element of a dictionary value (or None if the key doesn't exist):

def first_elem_of_value(record: dict, key: str):
    try:
        return record[key][0]
    except KeyError:
        return None

and then build the master dict with a single comprehension:

xml_master_dict = {
    key: [
        first_elem_of_value(record, key)
        for record in data
    ]
    for key in ('name', 'email', 'phone', 'ssn')
}
>>> xml_master_dict
{'name': ['mallesh', 'bhavik', 'jagarini', 'mallesh'], 'email': ['m@gmail.com', None, 'm@gmail.com', 'm@gmail.com'], 'phone': [None, None, '111111', '1234556'], 'ssn': [None, '1000011', None, '10000012']}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM