0

I'd like to change a value in highly nested dictionary. I am at the stage when I've all needed paths in my list in given structure :

['Assets', 'Coop', 0, 'Room', 24, 'Id']

If I wanted to access the value of the dictionary manually I'd call it via

dict['Assets']['Coop'][0]['Room][24]['Id']

I've tried to achive my goal with simple recursion function however I'm still a beginner and actually am stuck

def set_in_dict(path, config, i, l):
    if i == l-1:
        config = 123123123123
    else: 
        return set_in_dict(path,config[path[i]], i+1, l)

At this point my recursion function in this case reach the end of list, config become value of key : 'Id' and i change it to another value, however it changes only localy, globaly it's the same. I wonder if I can access and set value of my dict with some trick like conversion the list to the string in following format:

some_str = ['Assets']['Coop'][0]['Room][24]['Id']
dict[some_str] = 550
  • 1
    I think there are some answers on this similar question that will do the trick: [Access nested dictionary items via a list of keys?](https://stackoverflow.com/questions/14692690/access-nested-dictionary-items-via-a-list-of-keys) – JNevill Mar 01 '22 at 15:50
  • It seems working for now :D I hope it's gonna work for every case <3 – Szymon Kołodziej Mar 01 '22 at 16:28

0 Answers0