Bottom Page

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
}]
}
Quote
#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
Age Doesn't Matter, Your Passion Does
- Shreya Jain
Quote
#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

Life of Brian: Conjugate the verb, "to go" !
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  list comprehension : print column as row pyseeker 3 70 Yesterday, 09:06 AM
Last Post: newbieAuggie2019
  question about list comprehension Exsul 3 78 Yesterday, 08:27 AM
Last Post: DeaD_EyE
  How to count and order numbers in a list rachyioli 2 93 Aug-21-2019, 10:51 AM
Last Post: perfringo
  weird result trying to remove numbers from a list Exsul 2 80 Aug-21-2019, 06:45 AM
Last Post: perfringo
  Convert a mongo db scrip to python equivalent Herath 1 84 Aug-20-2019, 02:28 PM
Last Post: fishhook
  How to remove multiples in a list jasper100125 6 179 Aug-20-2019, 06:38 AM
Last Post: buran
  Pass Dictionary to Method is not possible Frank123456 1 82 Aug-19-2019, 10:18 AM
Last Post: buran
  Dictionary for Excel Data Ranjirock 0 90 Aug-18-2019, 05:26 PM
Last Post: Ranjirock
  How can I increment a List item with in a "for in" msteffes 4 301 Aug-14-2019, 08:55 AM
Last Post: DeaD_EyE
  Basic one: Aggregating from a dictionary Mustey 1 129 Aug-12-2019, 07:33 PM
Last Post: buran

Forum Jump:


Users browsing this thread: 1 Guest(s)