i tried to join this 2 dictionary
company_detail_list = {
'Company 1': {
'name': 'Company 1',
'domain': 'Retail'
},
'Company 2': {
'name': 'Company 2',
'domain': 'Construction'
}
}
employee_detail_list = {
'John Doe': {
'full_name': 'John Doe',
'company': 'Company 1'
},
'Tom Smith': {
'full_name': 'Tom Smith',
'company': 'Company 2'
}
}
now the problem is, how do i join 2 dictionary with key of "Company" to get "domain" from dict1 to make this output:
[{
"full_name": "John Doe",
"company": "Company 1",
"domain": "Retail"
}, {
"full_name": "Tom Smith",
"company": "Company 2",
"domain": "Construction"
}
}]
Thanks before
CodePudding user response:
You can use dict.get()
and dict.update()
and create your list
of dict
s like the below:
lst = []
for k, v in employee_detail_list.items():
dct_tmp = v
dct_tmp.update({'domain' : company_detail_list.get(v['company'], {}).get('domain')})
lst.append(dct_tmp)
print(lst)
Output:
[
{'full_name': 'John Doe', 'company': 'Company 1', 'domain': 'Retail'},
{'full_name': 'Tom Smith', 'company': 'Company 2', 'domain': 'Construction'}
]