Last active
October 17, 2019 14:20
-
-
Save thorwhalen/cdb8521952dae638466a3b79db9f67ac to your computer and use it in GitHub Desktop.
Lambda function to get an element in a nested dict, specifying a dot separated key path (and generalization to any separator and method)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from functools import reduce | |
# The simple dotpath version | |
dotpath_get = lambda d, dotpath: reduce(lambda x, y: x.get(y), dotpath.split('.'), d) | |
# A generalization to any separator and any "get" method: | |
keypath_get = lambda d, keypath, sep, getmethod: reduce(lambda x, y: getattr(x, getmethod)(y), keypath.split(sep), d) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage