1

Consider this dictionary shown below:

"Title": {
"Label-1": {
    "sublabel-1": {
        "item1": {}, 
        "item2": {}
        }, 
    "sublabel-2": {
        "item3": {}, 
        "item4": {}, 
        "item5": {}
        }
    }
"Label-2": {
    "sublabel-1": {
        "item6": {}, 
        "item7": {}, 
        "item8": {}, 
        }
    }
}

Here is a dictionary to modify some key-value pairs as shown below.

values_to_add={item1:a,item2:b}

The goal is to add values a and b to the respective keys in the dictionary.

For reference, I read the following posts:

1.First Reference: (The difference is that I just have the key-value pair to modify and not the detailed trail upto the key-value pair as given in the update variable.)

2.Second Reference: (Unfortunately, no follow up on the question.)

Some additional points:

  1. Level and depth of dictionary may vary. The main focus would be to add values to the innermost keys for any sublabel most of the times.(in this case item1 and item2)

  2. Using python 2 to manipulate the dictionary.

I am currently trying to create a program to work it out. Hopefully looking to get some insights on different approaches to the problem and learn something new.

lyxαl
  • 1,108
  • 1
  • 16
  • 26
UserA
  • 47
  • 9
  • 1
    There is no magic involved. You need to search your whole structure until you find a matching keyname, then insert your values there. SO is about fixing _your_ Code, show it to us.If you have specifiic questions provide your code as [mvce](https://stackoverflow.com/help/mcve). If you encounter errors, copy and paste the error message verbatim ( word for word) into your question. Avoid using screenshots unless you need to convey layout errors. We can NOT copy and paste your image into our IDEs to fix your code. – Patrick Artner Apr 04 '18 at 08:06
  • Just iterate the dictionary. If you do this often, a dictionary *may* not be the ideal structure for you. You may wish to implement your own class which "records" the location of each leaf-level key. – jpp Apr 04 '18 at 08:11
  • As BoarGules mentions in his (excellent) answer, this can only be expected to "work" if either your leaf nodes are garanteed to be unique OR if all leaf nodes with the same name should be updated. – bruno desthuilliers Apr 04 '18 at 10:45

1 Answers1

1

It looks like your data structure reflects the way you want to output the data, and that is complicating the update process.

Postpone the construction of your nested dictionary. Instead, set up flat a dictionary with only the leaf keys, and the superordinate keys as data:

{
"item1": {'path': ("Title","Label-1","sublabel-1"), "values":{} }, 
"item2": {'path': ("Title","Label-1","sublabel-1"), "values":{} }, 
"item3": {'path': ("Title","Label-1","sublabel-2"), "values":{} },  
"item4": {'path': ("Title","Label-1","sublabel-2"), "values":{} }, 
"item5": {'path': ("Title","Label-1","sublabel-2"), "values":{} },
"item6": {'path': ("Title","Label-2","sublabel-1"), "values":{} }, 
"item7": {'path': ("Title","Label-2","sublabel-1"), "values":{} },  
"item8": {'path': ("Title","Label-2","sublabel-1"), "values":{} },  
}

When it is complete you can iterate through it and construct the nested structure you want. This also allows you to easily accommodate key paths of varying lengths, which would otherwise make for a very complex access method.

This does of course assume your leaf keys are unique. That was also implicit in your question. It does strike me as an unlikely assumption in practice, though.

BoarGules
  • 16,440
  • 2
  • 27
  • 44
  • @ BoarGules: Thank you for the insight. Agreed that it is much simpler to access and modify a flatten dictionary. Just a clarification would the approach of flattening nested dictionaries be useful in question posted in second reference link? – UserA Apr 05 '18 at 05:41
  • Mmm, maybe. In that question OP wanted to do CRUD operations on the nested structure. For that, you *could* arrange for the flattened dict and the nested dict to refer to the same leaf-level *value*-dicts (as in `a={}; b=a`) and get the best of both worlds. I wouldn't recommend that because little mistakes could cause very bad, hard-to-find bugs, with Create and Delete being particularly hard to get right. – BoarGules Apr 05 '18 at 09:02