Search code examples
listdictionarynestedkeynames

How to access Keys/Names in nested Dictionaries


I have following nested dictionary:

nested_dict = { 'dict1': {'fun1'     : {"a"        :1,
                                        "b"        :2},
                          'fun2'     : {"c"        :3,
                                        "d"        :4}
                'dict2': {'fun3'     : {"e"        :5,
                                        "f"        :6},
                          'fun4'     : {"g"        :7,
                                        "h"        :8}}}

How can I extract e.g. name/key "dict2" of the outer dictionary and the key/name "fun4" of the nested dictionary and store them to the other variables "outerDictName" and "innerDictName"?

I checked couple of post to this topics, but couldn't figure out the correct way/syntax.

Thx, Toby


Solution

  • keys1=list(nested_dict.keys())
    keys2=list(nested_dict["dict1"].keys())