'How to group some keys of dictionary to nested key?
I have a dictionaries of list
[{
'LoanID' : 001,
'Applicant_Income.1' : 5000,
'Applicant_Occupation.1' : "Manager",
'Applicant_Income.2' : 3000,
'Applicant_Occupation.2' : "Accountant",
'LoanAmount' : 10000
},
{
'LoanID' : 002,
'Applicant_Income.1' : 6000,
'Applicant_Occupation.1' : "Senior Manager",
'Applicant_Income.2' : 2000,
'Applicant_Occupation.2' : "Services",
'LoanAmount' : 8000
}]
I need to create a new key 'Applicant' and move 'Applicant_Income' and 'Applicant_Occupation' inside that, result each dict in list looks like
{
'LoanID' : 001,
'Applicant' : [
{
'Applicant_Income' : 5000,
'Applicant_Occupation' : "Manager"
}'
{
'Applicant_Income' : 3000,
'Applicant_Occupation' : "Accountant"
}]
'LoanAmount' : 10000
},
{
'LoanID' : 002,
'Applicant' : [
{
'Applicant_Income' : 6000,
'Applicant_Occupation' : "Senior Manager"
}'
{
'Applicant_Income' : 2000,
'Applicant_Occupation' : "Services"
}]
'LoanAmount' : 8000
}
Solution 1:[1]
This should work (info is the list you defined above):
for d in range(len(info)):
info[d]['Applicant'] = [{'Applicant_Income.1': info[d]['Applicant_Income.1'], 'Applicant_Occupation.1': info[d]['Applicant_Occupation.1']}, {'Applicant_Income.2': info[d]['Applicant_Income.2'], 'Applicant_Occupation.2': info[d]['Applicant_Occupation.2']}]
del info[d]['Applicant_Income.1']
del info[d]['Applicant_Occupation.1']
del info[d]['Applicant_Income.2']
del info[d]['Applicant_Occupation.2']
Output:
[{'LoanID': '002',
'LoanAmount': 10000,
'Applicant': [{'Applicant_Income.1': 5000,
'Applicant_Occupation.1': 'Manager'},
{'Applicant_Income.2': 3000, 'Applicant_Occupation.2': 'Accountant'}]},
{'LoanID': '002',
'LoanAmount': 8000,
'Applicant': [{'Applicant_Income.1': 6000,
'Applicant_Occupation.1': 'Senior Manager'},
{'Applicant_Income.2': 2000, 'Applicant_Occupation.2': 'Services'}]}]
Solution 2:[2]
The other answer only works if the number of values for income and occupation is fixed to two.
Here is a more dynamic solution:
orgList is the list you define above
finalList is the list you want
def getApplicant(entry):
tempAppList = []
for key in [key for key in entry if 'Applicant_Income' in key]:
tempAppDict = {}
tempAppDict['Applicant_Income'] = entry[key]
tempAppDict['Applicant_Occupation'] = entry[key.replace('Income', 'Occupation')]
tempAppList.append(tempAppDict)
return tempAppList
finalList = []
for entry in orgList:
tempDict = {}
tempDict['LoanID'] = entry['LoanID']
tempDict['Applicant'] = getApplicant(entry)
tempDict['LoanAmount'] = entry['LoanAmount']
finalList.append(tempDict)
Output:
[
{
"LoanID": "001",
"Applicant": [
{
"Applicant_Income": 5000,
"Applicant_Occupation": "Manager"
},
{
"Applicant_Income": 3000,
"Applicant_Occupation": "Accountant"
}
],
"LoanAmount": 10000
},
{
"LoanID": "002",
"Applicant": [
{
"Applicant_Income": 6000,
"Applicant_Occupation": "Senior Manager"
},
{
"Applicant_Income": 2000,
"Applicant_Occupation": "Services"
}
],
"LoanAmount": 8000
}
]
Note: Leading zeros are not allowed on integers (like you have on LoadId). So you need to use a string for that
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | catasaurus |
| Solution 2 |
