Reputation: 13
I am using Python 3.6
I have a list of dictionaries (below) and would like to return the following values: id
, name
, color
for every record in my data. I cannot seem to figure out how to iterate through all the "pages" in my dataset to return all values. I have gotten as far as the for
loop included below, but that only returns data for the first page and none of the others. The end-state I'm looking for would return the data in the structure I have in my for
loop but for all pages.
My data and for loop:
mydict = [
{
"page": 1,
"data":[
{
"id": 11111,
"name": "smith",
"color": "orange",
"subsidiary": "no"
},
{
"id": 22222,
"name": "smith",
"color": "orange",
"subsidiary": "yes",
"subsidiaries": [
{
"id": 33333,
"name": "alpha",
"color": "blue"
},
{
"id": 44444,
"name": "alpha",
"color": "blue"
}
],
"last_updated": 123456789
}
]
},
{
"page": 2,
"data":[
{
"id": 55555,
"name": "smith",
"color": "orange",
"subsidiary": "no"
},
{
"id": 66666,
"name": "smith",
"color": "orange",
"subsidiary": "yes",
"subsidiaries": [
{
"id": 77777,
"name": "alpha",
"color": "blue"
},
{
"id": 88888,
"name": "alpha",
"color": "blue"
}
],
"last_updated": 987654321
}
]
}
]
for i in mydict[0]['data']:
print ({"cust_id": i['id'], "cust_name": i['name'], "fabric_color": i['color']})
Returns:
{'cust_id': 11111, 'cust_name': 'smith', 'fabric_color': 'orange'}
{'cust_id': 22222, 'cust_name': 'smith', 'fabric_color': 'orange'}
Upvotes: 1
Views: 78
Reputation: 164643
You have a list of dictionaries, not a dictionary of lists. So you can use a nested for
loop:
for d in mydict:
for i in d['data']:
print({"cust_id": i['id'], "cust_name": i['name'], "fabric_color": i['color']})
To store results in a list of dictionaries instead of just printing, you can append to a list:
L = []
for d in mydict:
for i in d['data']:
L.append({"cust_id": i['id'], "cust_name": i['name'], "fabric_color": i['color']})
More efficiently, you can use an equivalent list comprehension:
L = [{"cust_id": i['id'], "cust_name": i['name'], "fabric_color": i['color']} \
for d in mydict for i in d['data']]
print(L)
[{'cust_id': 11111, 'cust_name': 'smith', 'fabric_color': 'orange'},
{'cust_id': 22222, 'cust_name': 'smith', 'fabric_color': 'orange'},
{'cust_id': 55555, 'cust_name': 'smith', 'fabric_color': 'orange'},
{'cust_id': 66666, 'cust_name': 'smith', 'fabric_color': 'orange'}]
Here's an example which captures subsidiary data if it exists:
L = []
for d in mydict:
for i in d['data']:
L.append({"cust_id": i['id'], "cust_name": i['name'], "fabric_color": i['color']})
if 'subsidiaries' in i:
for s in i['subsidiaries']:
L.append({"cust_id": s['id'], "cust_name": s['name'], "fabric_color": s['color']})
print(L)
[{'cust_id': 11111, 'cust_name': 'smith', 'fabric_color': 'orange'},
{'cust_id': 22222, 'cust_name': 'smith', 'fabric_color': 'orange'},
{'cust_id': 33333, 'cust_name': 'alpha', 'fabric_color': 'blue'},
{'cust_id': 44444, 'cust_name': 'alpha', 'fabric_color': 'blue'},
{'cust_id': 55555, 'cust_name': 'smith', 'fabric_color': 'orange'},
{'cust_id': 66666, 'cust_name': 'smith', 'fabric_color': 'orange'},
{'cust_id': 77777, 'cust_name': 'alpha', 'fabric_color': 'blue'},
{'cust_id': 88888, 'cust_name': 'alpha', 'fabric_color': 'blue'}]
Upvotes: 1
Reputation: 2565
Do something like this.
mydict = []
for page in mydict:
print(page['page'])
for data in page['data']:
print ({"cust_id": data['id'], "cust_name": data['name'], "fabric_color":data['color']})
You were only iterating the first index of the dictionary when there are more than 1 element.
Upvotes: 0