I would like to be able to use a single "nested_key" variable to get a value from an (arbitrarily deep) set of nested dicts.
If I have a dict of dicts I can simply index it like so:
a = {"Alice": {"Car": {"Color": "Blue"}}}
a["Alice"]["Car"]["Color"]
>>> "Blue"
But I don't think(?) I can easily make that chain of indexes into a variable and use it in the same way?
a = {"Alice": {"Car": {"Color": Blue}}}
nested_key = ["Alice"]["Car"]["Color"] # for use in: a[nested_key]
>>> TypeError: list indicies must be integer, not str
Lists have the slice()
object; but is there a way to assign a 'key-like' object to a variable?
Otherwise I suppose a recursive_get
style function (e.g. https://stackoverflow.com/a/28225660/2588039) is probably my best bet?