Rui Huang
Rui Huang

Reputation: 65

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

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

Upvotes: 2

Views: 5722

Answers (2)

Chetan Kabra
Chetan Kabra

Reputation: 361

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)

Upvotes: 1

hiro protagonist
hiro protagonist

Reputation: 46899

a minor change of the code you link to yields the results:

def fun(dct, value, path=()):

    for key, val in dct.items():
        if val == value:
            yield path + (key, )
    for key, lst in dct.items():
        if isinstance(lst, list):
            for item in lst:
                for pth in fun(item, value, path + (key, )):
                    yield pth

for your input:

for item in fun(dct, value='blah blah blah'):
    print(item)

# ('nestedlist', 'nestednestedlist', 'keyA')
# ('nestedlist', 'nestednestedlist', 'keyZ')

update after your comment: a minor change of the code can do what you want:

def fun(dct, value, path=()):

    for key, val in dct.items():
        if val == value:
            yield path + (val, )
    for key, lst in dct.items():
        if isinstance(lst, list):
            for item in lst:
                for pth in fun(item, value, path + (dct['id'], key, )):
                    yield pth

example:

for item in fun(dct, value='xyz'):
    print(item)
# ('abcde', 'nestedlist', 'qwerty', 'nestednestedlist', 'xyz')

Upvotes: 3

Related Questions