Created
December 14, 2020 14:19
-
-
Save swateek/406f10ece1101658237d485b123d5789 to your computer and use it in GitHub Desktop.
Python Snippets
This file contains hidden or 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
''' | |
https://stackoverflow.com/a/43491315/1844056 | |
''' | |
def keys_exists(element, *keys): | |
''' | |
Check if *keys (nested) exists in `element` (dict). | |
''' | |
if not isinstance(element, dict): | |
raise AttributeError('keys_exists() expects dict as first argument.') | |
if len(keys) == 0: | |
raise AttributeError('keys_exists() expects at least two arguments, one given.') | |
_element = element | |
for key in keys: | |
try: | |
_element = _element[key] | |
except KeyError: | |
return False | |
return True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment