So I am pulling dictionary formatted JSONs from a web service to get various metrics about specific products. I have nested loops which pull pull nested dictionaries, and I would like to assign these nested dictionaries to the unique products (which are keys). To make it simple I have some example code which displays the issue I am encountering (though nonsensical, bananas are amazing).
import random
test = {}
fruit = ['apple','banana','watermelon']
color = ['red','blue','green']
shape = ['oval','round','long']
taste = ['good','bad','fresh']
farm = [{'US': 'GA'}, {"Peru": "Lima"},{'US': 'New York'}] # this data in my use is actually more varied
for i in fruit:
this_color = random.choice(color) # these represent making URLs to pull from
this_shape = random.choice(shape)
this_taste = random.choice(taste)
description = [this_color,this_shape,this_taste]
for x in description:
# this line would be pulling requests of dictionary formatted JSONs from the URLS
test.update({i:{x: random.choice(farm)}})
This give the following output:
{ 'apple': {'fresh': {'US': 'GA'}},
'banana': {'bad': {'Peru': 'Lima'}},
'watermelon': {'fresh': {'Peru': 'Lima'}}}
My issue: I am looking to get a dictionary that contains each descriptor [this_color, this_shape, this_taste]
as a key but I only get the last item this_taste
as a key. I would wager it is because I am using .update
incorrectly, but I am not sure the "correct" way to implement this. Any advice would be great!