'How to iterate through a nested dictionary and return the keys and values?

Say I have a nested dictionary like so:

dict = ["{'model': 'network.customer', 'pk': 'C00001', 'fields': {'name': 'Valentino Solomon', 'latitude': 57.13514, 'longitude': -2.11731}}" 
"{'model': 'network.customer', 'pk': 'C00002', 'fields': {'name': 'Luna Armstrong', 'latitude': 57.13875, 'longitude': -2.09089}}" 
"{'model': 'network.customer', 'pk': 'C00003', 'fields': {'name': 'Jaylen Crane', 'latitude': 57.101, 'longitude': -2.1106}}" 
"{'model': 'network.customer', 'pk': 'C00004', 'fields': {'name': 'Christopher Fritz', 'latitude': 57.10801, 'longitude': -2.23776}}" 
"{'model': 'network.customer', 'pk': 'C00005', 'fields': {'name': 'Timothy Hutchinson', 'latitude': 57.10076, 'longitude': -2.27073}}" 
"{'model': 'network.customer', 'pk': 'C00006', 'fields': {'name': 'Yesenia Reeves', 'latitude': 57.13868, 'longitude': -2.16525}}" 
"{'model': 'network.customer', 'pk': 'C00007', 'fields': {'name': 'Cameron Vargas', 'latitude': 57.16115, 'longitude': -2.15543}}"]

How can I iterate through this to get the fields pk and the keys and values inside the key fields so that the return is:

data = "{'pk': 'C00001', 'name': 'Valentino Solomon', 'latitude': 57.13514, 'longitude': -2.11731}",
"{'pk': 'C00002', 'name': 'Luna Armstrong', 'latitude': 57.13875, 'longitude': -2.09089}",
"{'pk': 'C00003', 'name': 'Jaylen Crane', 'latitude': 57.101, 'longitude': -2.1106}",
"{'pk': 'C00004', 'name': 'Christopher Fritz', 'latitude': 57.10801, 'longitude': -2.23776}",
"{'pk': 'C00005', 'name': 'Timothy Hutchinson', 'latitude': 57.10076, 'longitude': -2.27073}"

Thanks!

I can access the fields by using this:

print(customers[0]['fields'])


Solution 1:[1]

Convert that dict into a list and make every entry a dict. Then just iterate through the list, put it in a dict and access it like you would every other dict.

list: list = [{'model': 'network.customer', 'pk': 'C00001', 'fields': {'name': 'Valentino Solomon', 'latitude': 57.13514, 'longitude': -2.11731}}, 
{'model': 'network.customer', 'pk': 'C00002', 'fields': {'name': 'Luna Armstrong', 'latitude': 57.13875, 'longitude': -2.09089}}, 
{'model': 'network.customer', 'pk': 'C00003', 'fields': {'name': 'Jaylen Crane', 'latitude': 57.101, 'longitude': -2.1106}}, 
{'model': 'network.customer', 'pk': 'C00004', 'fields': {'name': 'Christopher Fritz', 'latitude': 57.10801, 'longitude': -2.23776}}, 
{'model': 'network.customer', 'pk': 'C00005', 'fields': {'name': 'Timothy Hutchinson', 'latitude': 57.10076, 'longitude': -2.27073}}, 
{'model': 'network.customer', 'pk': 'C00006', 'fields': {'name': 'Yesenia Reeves', 'latitude': 57.13868, 'longitude': -2.16525}}, 
{'model': 'network.customer', 'pk': 'C00007', 'fields': {'name': 'Cameron Vargas', 'latitude': 57.16115, 'longitude': -2.15543}}]

for entry in list:
    dict: dict = entry
    print(dict['pk'])
    print(dict['fields']['name'])

If anybody has a similar list like OP, where all the dicts are in one single string (convert string of dicts without commas into list of dicts), you can use this bit of code: import ast

def convertToList(inString: str):
    i: int = 0
    closeCounter: int = 0
    openCounter: int = 0
    firstOpen: int = 0
    outList: list = []
    while i < len(inString):
        openPos = inString.find("{", i)
        closePos = inString.find("}", i)
        if closePos == -1:
            return outList
        if openPos < closePos and openPos != -1:
            openCounter += 1
            if openCounter == 1:
                firstOpen = i
            i = openPos + 1
        elif closePos < openPos or openPos == -1:
            closeCounter += 1
            if openCounter == closeCounter:
                dict: dict = ast.literal_eval(inString[firstOpen:closePos+1])
                outList.append(dict)
                openCounter = 0
                closeCounter = 0
            i = closePos + 1
    return outList

                

list: list = ["{'model': 'network.customer', 'pk': 'C00001', 'fields': {'name': 'Valentino Solomon', 'latitude': 57.13514, 'longitude': -2.11731}}" 
"{'model': 'network.customer', 'pk': 'C00002', 'fields': {'name': 'Luna Armstrong', 'latitude': 57.13875, 'longitude': -2.09089}}" 
"{'model': 'network.customer', 'pk': 'C00003', 'fields': {'name': 'Jaylen Crane', 'latitude': 57.101, 'longitude': -2.1106}}" 
"{'model': 'network.customer', 'pk': 'C00004', 'fields': {'name': 'Christopher Fritz', 'latitude': 57.10801, 'longitude': -2.23776}}" 
"{'model': 'network.customer', 'pk': 'C00005', 'fields': {'name': 'Timothy Hutchinson', 'latitude': 57.10076, 'longitude': -2.27073}}" 
"{'model': 'network.customer', 'pk': 'C00006', 'fields': {'name': 'Yesenia Reeves', 'latitude': 57.13868, 'longitude': -2.16525}}" 
"{'model': 'network.customer', 'pk': 'C00007', 'fields': {'name': 'Cameron Vargas', 'latitude': 57.16115, 'longitude': -2.15543}}"]

list0 = convertToList(list[0])
print(list0)
for entry in list0:
    dict: dict = entry
    print(dict['pk'])
    print(dict['fields']['name'])

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1