-2

I need to make a method that lets you navigate dicts of various depths using a simple string. How can I make get_from_dict(dict, "images.player.left") work? thaks

CiY3
  • 1
  • 3

1 Answers1

0

Split the string, and then look up the keys in a loop.

>>> def get_from_dict(d, keys):
...     for key in keys.split("."):
...         d = d[key]
...     return d
...
>>> get_from_dict({"images": {"player": {"left": "tada!"}}}, "images.player.left")
'tada!'
Samwise
  • 68,105
  • 3
  • 30
  • 44