Skip to content

Instantly share code, notes, and snippets.

@rootcss
Created April 26, 2017 10:32
Show Gist options
  • Select an option

  • Save rootcss/8c4f219237a32419557383503f1d556f to your computer and use it in GitHub Desktop.

Select an option

Save rootcss/8c4f219237a32419557383503f1d556f to your computer and use it in GitHub Desktop.
"""
Fetches key paths and values of a dictionary
"""
def dict_path(path, my_dict):
for k,v in my_dict.iteritems():
if isinstance(v, dict):
dict_path(path + "_" + k, v)
else:
output.append('key: {}_{}, val: {}'.format(path, k, v))
event = {
"params": {
"a": "abc",
"b": "17976",
"c": {
"d": "abcdef",
"e": "17976",
"f": "Hello World"
},
"a": "api/v1/",
"b": "hello"
}
}
output = []
dict_path('', event)
print output
# ['key: _params_a, val: api/v1/', 'key: _params_c_e, val: 17976', 'key: _params_c_d, val: abcdef', 'key: _params_c_f, val: Hello World', 'key: _params_b, val: hello']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment