Search code examples
pythonlistdictionaryjson-normalize

How to access nested dictionary from a list of dictionaries


I have a list of dictionaries (sorry It's a bit complex but I'm trying to display the real data) :

[{'alerts': [{'city': ' city name1',
              'country': 'ZZ',
              'location': {'x': 1, 'y': 3},
              'milis': 1582337463000},
             {'city': ' city name2',
              'country': 'ZZ',
              'location': {'x': 1, 'y': 3},
              'pubMillis': 1582337573000,
              'type': 'TYPE2'}],
  'end': '11:02:00:000',
  'start': '11:01:00:000'},
 {'alerts': [{'city': ' city name3',
              'country': 'ZZ',
              'location': {'x': 1, 'y': 3},
              'milis': 1582337463000}],
  'end': '11:02:00:000',
  'start': '11:01:00:000'}]

In general the list structure is like this :

[
{ [
    { {},
    },
    { {},
    }
  ],
},
{ [
    { {},
    },
    { {},
    }
  ],
}
]

If I want to access city name1, I can access using this line of code : alerts[0]['alerts'][0]['city'].

If I want to access city name2, I can access using this code : alerts[0]['alerts'][1]['city'].

How can I access this in a loop?


Solution

  • Use pandas

    • data equals your sample list of dicts
    import pandas as pd
    
    # create the dataframe and explode the list of dicts
    df = pd.DataFrame(data).explode('alerts').reset_index(drop=True)
    
    # json_normalize the dicts and join back to df
    df = df.join(pd.json_normalize(df.alerts))
    
    # drop the alerts column as it's no longer needed
    df.drop(columns=['alerts'], inplace=True)
    
    # output
              start           end country         city         milis  location.x  location.y   type     pubMillis
    0  11:01:00:000  11:02:00:000      ZZ   city name1  1.582337e+12           1           3    NaN           NaN
    1  11:01:00:000  11:02:00:000      ZZ   city name2           NaN           1           3  TYPE2  1.582338e+12
    2  11:01:00:000  11:02:00:000      ZZ   city name3  1.582337e+12           1           3    NaN           NaN