1

I have FOR cycle for adding new value items and my data structure consist of nested dictionaries like this:

d = {
    'item1': {
        'id1': {'a': 1}, 
        'id2': {'b': 2}
    }, 
    'item2': {
        'id7': {'o': 1}, 
        'id9': {'k': 2}
    }
}

I cannot understand how to append value of 'item1' with some dictionary data like this {'id3': {'z': 3}} (new data)

When I'm trying to update existing value (adding new sub-value) -

d['item1'] = {'id3': {'z': 3}}

the whole value is replacing with new data

So, the question is how to append existing sub-nested dictionary with new dictionary?

UPDATE

items = {item1, item2, item3}
d = {}
for item in items:

    id = item['id']
    name = item['name']
    description = item['description']
    language = item['language']

    d[language] = {id: {'name': name, 'description': description}}

As you can see on each iteration I'm replacing existing d[language] value for every item in items. But I would like to update value in the way to append value with data for each item.

Can you please tell me how to achieve that?

Quanti Monati
  • 769
  • 1
  • 11
  • 35

0 Answers0