1

I have looked but not found what I am looking for -- which is a Pythonic way of retrieving different parts of the tree knowing something at the lower levels.

I'm pretty sure it is because I am not asking the question correctly.

I have dictionary. The dictionary contains many things, among them a list of dictionaries. Those dictionaries have data, among them a list of dictionaries.

aa = {"ab": 23,            # aa is a dictionary
      "ac": "example",
      "ad": [{"ad_a": 37,   # ad is a list of dictionaries 
              "ad_b": "some text",
              "ad_c": [{"ad_c_a": 49,   # ad_c is a list of dictionaries 
                        "ad_c_b": "some other text",
                        "the key": "THE IMPORTANT THING",
                        "ad_c_d": "more stuff"
                        },
                       {"bd_c_a": 49,   
                        "bd_c_b": "some other text",
                        "bd_c_c": "some other text",
                        "bd_c_d": "more stuff"
                        }
                       {another dictionary},
                        ]
            },
            {another dictionary},
            {another dictionary}]
      }

Currently, I loop through the various levels dictionaries until I find THE IMPORTANT THING is in aa["ad"]["ad_c"]

That tell me which list element of ad_c I am in. Knowing that, what is the Pythonic way to retrieve the values from aa["ad"]["ad_c"]["ad_c_a"] and other items in the list element?

I don't know where I'll end up in ad until I've done the looping, but after THE IMPORTANT THING is found, I do.

Just writing this down has given me some ideas on what to try, but I'll post it anyway in case I don't find the answer right away.

-Craig

J. Craig Williams
  • 380
  • 1
  • 2
  • 18
  • 1
    You may get some inspiration from [this question](https://stackoverflow.com/questions/12507206/python-how-to-completely-traverse-a-complex-dictionary-of-unknown-depth). – DYZ Aug 22 '18 at 19:23
  • I'll reread that tomorrow but the first read through did not reveal the answer. If it does, I'll come back and say so. If not, I'll rephrase the question. – J. Craig Williams Aug 23 '18 at 00:06
  • I think that link and this one https://stackoverflow.com/questions/231767/what-does-the-yield-keyword-do will get me where I need to go. Thank you. – J. Craig Williams Aug 24 '18 at 13:42

0 Answers0