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?
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 ).
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With