Created
April 27, 2022 02:32
-
-
Save craigderington/733bf74874b252322a5a4fca02d21dd3 to your computer and use it in GitHub Desktop.
Recursive Dictionary Get
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
def recursive_dict_get(d, *keys, default_none=False): | |
"""Recursive dict get. Can take an arbitrary number of keys and returns an empty | |
dict if any key does not exist. https://stackoverflow.com/a/28225747""" | |
ret = reduce(lambda c, k: c.get(k, {}), keys, d) | |
if default_none and ret == {}: | |
return None | |
else: | |
return ret |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment