0

Hello I am parsing a swagger file from an yaml. In order to find the schema I have to parse "#components/schemas/Login"

so in the end I end up with a list -> ['components', 'schemas', 'Login']

My question

How can I get to that level using this list?

The point is to get to mydict['components']['schemas']['Login']

of course I can make a function

def get_to_dict_lvl_from_an_array(d, arr):
    for i in arr:
        d = d[i]
    return d

which does the job, but is this the right and an elegant solution?

chash
  • 3,975
  • 13
  • 29
ThunderHorn
  • 1,975
  • 1
  • 20
  • 42
  • 1
    Does this answer your question? [Access nested dictionary items via a list of keys?](https://stackoverflow.com/questions/14692690/access-nested-dictionary-items-via-a-list-of-keys) See also [this question](https://stackoverflow.com/questions/25833613/python-safe-method-to-get-value-of-nested-dictionary). – chash Jul 08 '20 at 16:32
  • @hmm yes it does, thank you! – ThunderHorn Jul 08 '20 at 16:37

1 Answers1

1

That's the only answer that comes to mind. Since the length is variable, I don't see any good non-iterative way to get to the end. I think it's elegant!

Andy Delworth
  • 137
  • 1
  • 12