Skip to content

Instantly share code, notes, and snippets.

@4poc
Created October 29, 2015 21:02
Show Gist options
  • Save 4poc/3e2243d3b9d57079ac2b to your computer and use it in GitHub Desktop.
Save 4poc/3e2243d3b9d57079ac2b to your computer and use it in GitHub Desktop.
foo = {
'nested': {
'dict': 42
}
}
query = 'nested.dict'
# reduce:
from functools import reduce
value = reduce(lambda accu, key: accu[key], query.split('.'), foo)
assert(value == 42)
# ???:
accu = foo
for key in query.split('.'):
accu = accu[key]
assert(accu == 42)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment