Python Forum
Convert List of Dictionary to dictionary of dictionary list in python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Convert List of Dictionary to dictionary of dictionary list in python
#1
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
}]
}
Reply
#2
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
Reply
#3
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}]}
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Modifying a dictionary recursively SpongeB0B 2 222 May-12-2024, 04:09 PM
Last Post: Gribouillis
  Strange behavior list of list mmhmjanssen 3 392 May-09-2024, 11:32 AM
Last Post: mmhmjanssen
  Sort a list of dictionaries by the only dictionary key Calab 2 686 Apr-29-2024, 04:38 PM
Last Post: Calab
Question Using Lists as Dictionary Values bfallert 8 516 Apr-21-2024, 06:55 AM
Last Post: Pedroski55
  Matching Data - Help - Dictionary manuel174102 1 453 Feb-02-2024, 04:47 PM
Last Post: deanhystad
  Dictionary in a list bashage 2 626 Dec-27-2023, 04:04 PM
Last Post: deanhystad
  filtering a list of dictionary as per given criteria jss 5 780 Dec-23-2023, 08:47 AM
Last Post: Gribouillis
  need to compare 2 values in a nested dictionary jss 2 951 Nov-30-2023, 03:17 PM
Last Post: Pedroski55
  python dictionary is that a bug ? rmangla 2 644 Sep-27-2023, 05:52 AM
Last Post: DPaul
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,317 Sep-24-2023, 05:03 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020