find the path of the targeted values in a nested python dictionary and list

Viewed 4116

I have an issue of finding the path of the targeted values in a nested python dictionary and list. for example, I have following dict, and my targeted value is "blah blah blah".

{ "id" : "abcde",
  "key1" : "blah",
  "key2" : "blah blah",
  "nestedlist" : [ 
    { "id" : "qwerty",
      "nestednestedlist" : [ 
        { "id" : "xyz",
          "keyA" : "blah blah blah" },
        { "id" : "fghi",
          "keyZ" : "blah blah blah" }],
      "anothernestednestedlist" : [ 
        { "id" : "asdf",
          "keyQ" : "blah blah" },
        { "id" : "yuiop",
          "keyW" : "blah" }] } ] } 

What I want to get is the path of this value in the nested dictionary and list. "nestedlist" - "nestednestedlist" - "keyA"

I found this code from Find all occurrences of a key in nested python dictionaries and lists and made some changes:

def find(key,dic_name):
    if isinstance(dic_name, dict):
        for k,v in dic_name.items():          
            if k == 'name' and v == key:
                yield v
            elif isinstance(v,dict):
                for result in find(key,v):
                    yield result
            elif isinstance(v,list):
                for d in v:
                    for result in find(key,d):
                        yield result

But it can only get the targeted value in the result but not the path. Can anyone help? Thanks a lot

2 Answers
def get_fun(example, value path = None):
    if path is None:
        path = []
    if example == value:
        print path
    elif isinstance(example, dict):
        for key in example.keys():
            path.append(key)
            get_fun(example[key], path)
            path.pop()
    elif isinstance(example, list):
        for i in example:
            get_fun(i, path)
Related