Search code examples
pythonjsondictionarygeojson

Selecting entries from deeply nested dictionary


I have a very deeply nested dictionary (geojson) which contains shapefiles with features. One of these features is 'month_num' by which I want to select the data. The problem is that this dictionary is deeply nested. The closest thing I came up with is the following:

list(filter(lambda country: ['features'][country]['properties']['month_num'] == 2, geojson_countries))

But this gives me the following error:

TypeError: list indices must be integers or slices, not str

The geojson file looks like this:

enter image description here

I want to be able to select all entries which have 'month_num' == 2.

Could anyone please help?


Solution

  • You might get along with

    dct = your_dict.copy()
    dct["features"] = [item for item in dct["features"] 
                       if item["properties"]["month_num"] == 2]