1

I want to know if there is a way to directly reference a nested dictionary given a list of keys.

for instance, say i have a dictionary: foo = {'bar':{'baz':{'biz':'somevalue'}}}

and say i have some list of keys:

keys = ['bar', 'baz']
or
keys = ['bar', 'baz', 'biz']

in the case keys is a 2 item list: i want to assign foo['bar']['baz']=somenewvalue OR in the case keys is a 3 item list: i want to assign foo['bar']['baz']['biz']=somenewvalue

how to do this with the list variable "keys" when i wont always know how many elements will be in the keys list?

the following is not valid code, but can you suggest what would be to give me the expected results that i am looking for? something like:

foo[keys]=somenewvalue; where the var keys was some list

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
tyson.junk
  • 53
  • 5

0 Answers0