Python Forum

Full Version: Convert List of Dictionary to dictionary of dictionary list in python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi ALl ,

Need your help in python code for the following output required. thanks in advance.

Source file:


[ { "Env" :"dit",
"id": 1,
"appId": "AP11232",
"health": true
},
{ "Env" :"dit",
"id": 2,
"appId": "AP11232",
"health": false
}
{
"Env" :"int",
"id": 1,
"appId": "AP11232",
"health": true
},
{
"Env" :"int",
"id": 2,
"appId": "AP11232",
"health": true
},
{
"Env" :"int",
"id": 3,
"appId": "AP11232",
"health": true
},
{
"Env" :"int",
"id": 4,
"appId": "AP11232",
"health": true
}
]

Output Required:


{
"dit": [
{
"id": 1,
"appId": "AP11232",
"health": true
},
{
"id": 2,
"appId": "AP11232",
"health": false
}
],
"int":[
{
"id": 1,
"appId": "AP11232",
"health": true
},
{
"id": 2,
"appId": "AP11232",
"health": true
},
{
"id": 3,
"appId": "AP11232",
"health": true
},
{
"id": 4,
"appId": "AP11232",
"health": true
}]
}
Ok I hope this does the trick -
outputDict = {}

randomDict = [{ "Env" :"dit",
"id": 1,
"appId": "AP11232",
"health": True
},
{ "Env" :"dit",
"id": 2,
"appId": "AP11232",
"health": False
},
{
"Env" :"int",
"id": 1,
"appId": "AP11232",
"health": True
},
{
"Env" :"int",
"id": 2,
"appId": "AP11232",
"health": True
},
{
"Env" :"int",
"id": 3,
"appId": "AP11232",
"health": True
},
{
"Env" :"int",
"id": 4,
"appId": "AP11232",
"health": True
}
]

for Dict in randomDict:
    key = Dict['Env']
    outputDict.update({key : [{'id' : Dict['id'], 'health' : Dict['health'], 'appId' : Dict['appId']}]})
    
print(outputDict)

What do you need this for
I observe that there is no comma in source file between second and third dictionary; true and false are not booleans but just names which you have to define. I inserted missing comma and changed 'health' values to booleans.

If we analyse source and expected output:

source = [ {"Env" :"dit", "id": 1, "appId": "AP11232", "health": True},
           {"Env" :"dit", "id": 2, "appId": "AP11232", "health": False},  
           {"Env" :"int", "id": 1, "appId": "AP11232", "health": True},
           {"Env" :"int", "id": 2, "appId": "AP11232", "health": True},
           {"Env" :"int", "id": 3, "appId": "AP11232", "health": True},
           {"Env" :"int", "id": 4, "appId": "AP11232", "health": True}
         ]

result =         {"dit": [{"id": 1, "appId": "AP11232", "health": True},
                          {"id": 2, "appId": "AP11232", "health": False}],
                  "int": [{"id": 1, "appId": "AP11232", "health": True},
                          {"id": 2, "appId": "AP11232", "health": True}, 
                          {"id": 3, "appId": "AP11232", "health": True},
                          {"id": 4, "appId": "AP11232", "health": True}]
We can define objective - "from every dictionary in source pop out key 'Env' and make its value key in a new dictionary with value type list and append remaining dictionary to this". Doing it this way we don't need to know what are the remaining keys in source dictionary, we just grab what we need and keep everything else as it is.

To avoid key errors in situations where keys don't exist one could use defaultdict from built-in collections module or dictionary method setdefault (I used setdefault as it doesn't require import):

d = {}
for row in source:
    d.setdefault(row.pop('Env'), []).append(row)
Output:
{'dit': [{'id': 1, 'appId': 'AP11232', 'health': True}, {'id': 2, 'appId': 'AP11232', 'health': False}], 'int': [{'id': 1, 'appId': 'AP11232', 'health': True}, {'id': 2, 'appId': 'AP11232', 'health': True}, {'id': 3, 'appId': 'AP11232', 'health': True}, {'id': 4, 'appId': 'AP11232', 'health': True}]}