For example I have an dictionary as below
demo_dict = {1:{2:{3:{4:5}}}}
Now i have the path of the value 5 in the list as below
path = [1,2,3,4]
now by using that path i want to make it as
demo_dict[1][2][3][4] # expected
For example I have an dictionary as below
demo_dict = {1:{2:{3:{4:5}}}}
Now i have the path of the value 5 in the list as below
path = [1,2,3,4]
now by using that path i want to make it as
demo_dict[1][2][3][4] # expected
You can use simple recursion:
demo_dict = {1:{2:{3:{4:5}}}}
path = [1,2,3,4]
def get_val(d, _path):
return d[_path[0]] if not _path[1:] else get_val(d[_path[0]], _path[1:])
print(get_val(demo_dict, path))
Output:
5