0

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!

Andrew D
  • 73
  • 1
  • 6
  • 2
    Look at what your code does. Your update line is the same as `test[i] = {x: random.choice(farm)}`. So, the "test" key is the fruit (`i`), and the key of the inside dict is one member of `description`. It's not clear to me what you wanted. You should show us what your perfect output would look like. – Tim Roberts May 20 '21 at 19:09
  • 1
    `update()` doesn't merge recursively, so it replaces the element with key `i`. – Barmar May 20 '21 at 19:09
  • 1
    [This](https://stackoverflow.com/questions/3232943/update-value-of-a-nested-dictionary-of-varying-depth) seems like it will answer your question. – deseuler May 20 '21 at 19:15

1 Answers1

1
import random
import collections.abc

#https://stackoverflow.com/questions/3232943/update-value-of-a-nested-dictionary-of-varying-depth
def update(d, u):
    for k, v in u.items():
        if isinstance(v, collections.abc.Mapping):
            d[k] = update(d.get(k, {}), v)
        else:
            d[k] = v
    return d

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
        update(test, {i: {x: random.choice(farm)}})
deseuler
  • 408
  • 2
  • 8