Skip to content

Instantly share code, notes, and snippets.

@gustavi
Created August 17, 2018 10:59
Show Gist options
  • Save gustavi/3ec55d12f470e304bfea6a7986242b6d to your computer and use it in GitHub Desktop.
Save gustavi/3ec55d12f470e304bfea6a7986242b6d to your computer and use it in GitHub Desktop.
explore dict
Python 2.7.13 (default, Nov 24 2017, 17:33:09)
[GCC 6.3.0 20170516] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> def foo(path, struct):
... value = struct
... for x in path.split('.'):
... value = value[x]
... return value
...
>>> d = {'a': {'b': {'c': 42}, 'd': list()}}
>>> foo('a.b.c', d)
42
>>> foo('a.d', d)
[]
>>> foo('a', d)
{'b': {'c': 42}, 'd': []}
>>>
def foo(path, struct):
value = struct
for x in path.split('.'):
value = value[x]
return value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment