def count_levels ( value ) : if not isinstance ( value , dict ) or len ( value ) == 0 : return 0 elif len ( value ) == 0 : return 0 #An emptu dict has 0 else : nextval = list ( value . values ( ) ) [ 0 ] return 1 + count_levels ( nextval )
python for dictionary of dictonaries how to know the levels
Count how many levels are in a dict : scalar list etc = 0 {} = 0 { a : 1 } = 1 { a : { b : 1 }} = 2 etc ...
def count_levels(value):
    """
        Count how many levels are in a dict:
        scalar, list etc = 0
        {} = 0
        {'a':1} = 1
        {'a' : {'b' : 1}} = 2
        etc...
    """
    if not isinstance(value, dict) or len(value) == 0:
        return 0
    elif len(value) == 0:
        return 0 #An emptu dict has 0
    else:
        nextval = list(value.values())[0]
        return 1 + count_levels(nextval)