Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print complete key path for all the values of a python nested dictionary

If below is my nested dictionary I want to parse through recursively and print all the values along with the complete path of the nested key.

my_dict = {'attr':{'types':{'tag':{'name':'Tom', 'gender':'male'},'category':'employee'}}}

Expected output:

Key structure : my_dict["attr"]["types"]["tag"]["name"]<br>
value : "Tom"<br>
Key structure : my_dict["attr"]["types"]["tag"]["gender"]<br>
value : "male"<br>
Key structure : my_dict["attr"]["types"]["category"]<br>
value : "employee"<br>

I wrote a recursive function, but running to this:

my_dict = {'attr':{'types':{'tag':{'name':'Tom','gender':'male'},'category':'employee'}}}

def dict_path(path,my_dict):
    for k,v in my_dict.iteritems():
        if isinstance(v,dict):
            path=path+"_"+k
            dict_path(path,v)
        else:
            path=path+"_"+k
            print path,"=>",v

    return
dict_path("",my_dict)

Output:

_attr_types_category => employee
_attr_types_category_tag_gender => male
_attr_types_category_tag_gender_name => Tom

In the above : For male, the key struct shouldnt contain "category" How to retain the correct key structure?

like image 896
Suren Avatar asked Jan 17 '16 09:01

Suren


People also ask

How do I get values from nested dictionaries?

Access Values using get() Another way to access value(s) in a nested dictionary ( employees ) is to use the dict. get() method. This method returns the value for a specified key. If the specified key does not exist, the get() method returns None (preventing a KeyError ).

How do you print all keys in a list of a dictionary in Python?

Python's dict. keys() method can be used to retrieve the dictionary keys, which can then be printed using the print() function. A view object that shows a list of every key in the dictionary is the result of the dict. keys() method.

How do you get all values from a Python dictionary?

Python has a built-in method called values() that returns a view object. The dictionary's values are listed in the view object. We can use the values() method in Python to retrieve all values from a dictionary.

How do you find the value of all keys in a dictionary?

Use dict.get() to get the default value for non-existent keys. You can use the get() method of the dictionary ( dict ) to get any default value without an error if the key does not exist. Specify the key as the first argument.


1 Answers

As catavaran mentions, your problem is caused by adding the new path component to the path variable inside your for loop. You need to put the new path into the call so it gets passed to the next level of recursion and doesn't interfere with the path of subsequent items in the for loop at the current recursion level.

Here's an alternate solution that uses a recursive generator, rather than printing the results inside the dict_path function. (FWIW, I used print json.dumps(my_dict, indent=4) to reformat the dictionary).

my_dict = {
    "attr": {
        "types": {
            "category": "employee", 
            "tag": {
                "gender": "male", 
                "name": "Tom"
            }
        }
    }
}

def dict_path(my_dict, path=None):
    if path is None:
        path = []
    for k,v in my_dict.iteritems():
        newpath = path + [k]
        if isinstance(v, dict):
            for u in dict_path(v, newpath):
                yield u
        else:
            yield newpath, v

for path, v in dict_path(my_dict):
    print '_'.join(path), "=>", v

output

attr_types_category => employee
attr_types_tag_gender => male
attr_types_tag_name => Tom
like image 149
PM 2Ring Avatar answered Nov 02 '22 14:11

PM 2Ring