Answers for "How to check whether a nested hash element exists in python"

0

How to check whether a nested hash element exists in python

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
Posted by: Guest on June-20-2021

Code answers related to "How to check whether a nested hash element exists in python"

Python Answers by Framework

Browse Popular Code Answers by Language