I have a nested default dict that looks like the following:
source["China"]["Beijing"] = {
"num_persons" : 1454324,
"num_cars" : 134
}
source["Greece"]["Athens"] = {
"num_persons" : 2332,
"num_cars" : 12
}
How do I transform the above nested dict into a list of records like the one below using glom :
result = [
{
'country' : 'China',
'city' : 'Beijing',
'num_persons' : 1454324,
'num_cars' : 134
},
{
'country' : 'Greece',
'city' : 'Athens',
'num_persons' : 2332,
'num_cars' : 12
}
]
I've looked at https://glom.readthedocs.io/en/latest/tutorial.html#data-driven-assignment, but I'm still stumped.
I don't think you need a package for that. Just a list comprehension would suffice.
(You need python 3.5+ for generalized unpacking
**info.)Output: