This post from 2012 provides a (possible) solution where an entry in the dictionary
my_dict = {'allkids':{'child1':{'hair':'blonde'},
'child2':{'hair':'black'},
'child3':{'hair':'red'},
'child4':{'hair':'brown'}}}
can be accessed using
mypath = ('allkids', 'child3', 'hair')
kidshair[mypath]
However, I get a KeyError: ('allkids', 'child3', 'hair')
in the kidshair[mypath]
line when trying this (using Python 3.9.7).
I would like to use a similar solution to set the value of an entry located at a given path. I specifically don't want a function to do that:
kidshair[mypath] = 'black' # this is what I want
set_value(dict=kidshair, path=mypath, value='black') # this is what I don't want
kidshair = set_value(dict=kidshair, path=mypath, value='black') # neither do I want this
I need to set the values programmatically. So given a dictionary and a path, how do I perform an operation similar to my_dict['allkids']['child1']['hair'] = 'Black'
?