Skip to content

Instantly share code, notes, and snippets.

@swateek
Created December 14, 2020 14:19
Show Gist options
  • Save swateek/406f10ece1101658237d485b123d5789 to your computer and use it in GitHub Desktop.
Save swateek/406f10ece1101658237d485b123d5789 to your computer and use it in GitHub Desktop.
Python Snippets
'''
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