Python Forum
Searching through Nested Dictionaries and Lists
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Searching through Nested Dictionaries and Lists
#1
Hi all,
I hope you can help me Smile

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')
Reply
#2
You where almost there, but yours isinstance conditions will never be True because key error is string not a dict

Try that:

srch = 'unitaryAuthArea'


def nested_extract(nested_sites, key):
    for k, v in nested_sites.items():
        if isinstance(nested_sites[k], dict):
            print('\n K is a dictionary')
            nested_extract(nested_sites[k], key)
        if isinstance(nested_sites[k], list):
            print('\n K is a list')
            for i in range(len(nested_sites[k])):
                if isinstance(nested_sites[k][i], dict):
                    print('\n K is an Inner dictionary')
                    nested_extract(nested_sites[k][i], 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')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  List all possibilities of a nested-list by flattened lists sparkt 1 878 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Best practices for searching lists of lists osxtra 1 1,500 Oct-24-2020, 07:34 PM
Last Post: Larz60+
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,312 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  Comparing items from 2 lists of dictionaries illwill 7 2,662 Sep-14-2020, 10:46 PM
Last Post: bowlofred
  Creating Nested Dictionaries Confusion gw1500se 2 2,079 May-18-2020, 11:16 PM
Last Post: gw1500se
  Unpacking nested lists yonatan776 1 2,160 Apr-14-2020, 08:50 PM
Last Post: buran
  Help: for loop with dictionary and nested lists mart79 1 1,834 Apr-12-2020, 02:52 PM
Last Post: TomToad
  Finding value in nested dictionaries with lists mart79 16 7,745 Mar-08-2020, 08:16 PM
Last Post: ndc85430
  nested dictionaries to CSV mart79 9 12,305 Jul-29-2019, 04:59 AM
Last Post: mart79
  Transform simplified dictionary to nested dictionaries bhojendra 1 2,324 Jul-02-2019, 02:05 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020