Jul-09-2020, 08:44 AM
Hi all,
I hope you can help me
I am accessing data via an API and I end up with data in nested dictionaries and lists.
I am trying to get a list of the locations by searching for the "unitaryAuthArea" in this nested set of data.
The only result I get is:
Sites is a dictionary.
I hope you can help me

I am accessing data via an API and I end up with data in nested dictionaries and lists.
I am trying to get a list of the locations by searching for the "unitaryAuthArea" in this nested set of data.
The only result I get is:
Sites is a dictionary.
sites={'Locations': {'Location':[{'elevation': '50.0', 'id': '14', 'latitude': '54.9375', 'longitude': '-2.8092', 'name': 'Carlisle Airport', 'region': 'nw', 'unitaryAuthArea': 'Cumbria'},{'elevation': '108.0', 'id': '355874', 'latitude': '52.415775', 'longitude': '-4.059387', 'name': 'Penglais School', 'region': 'wl', 'unitaryAuthArea': 'Ceredigion'},{'elevation': '75.0', 'id': '3930', 'latitude': '51.55138', 'longitude': '-2.55933', 'name': 'Almondsbury', 'region': 'sw', 'unitaryAuthArea': 'South Gloucestershire'},{'elevation': '22.0', 'id': '26', 'latitude': '53.3336', 'longitude': '-2.85', 'name': 'Liverpool John Lennon Airport', 'region': 'nw', 'unitaryAuthArea': 'Merseyside'}]}} srch='unitaryAuthArea' def nested_extract(nested_sites, key): for k, v in nested_sites.items(): if isinstance(k, dict): print('\n K is a dictionary') nested_extract(k,key) if isinstance(k, list): print('\n K is a list') if isinstance([0], dict): print('\n K is an Inner dictionary') nested_extract([0],key) if k == key: print(v) if isinstance(sites, dict): print('\n Sites is a dictionary') nested_extract(sites,srch) else: print('\n Sites is NOT a dictionary')