def get_nested(d, list_of_keys, default):
    for k in list_of_keys:
        if k not in d:
            return default
d = d[k]
return d
print(get_nested(d2, ['ham', 'spam'], 99))
print(get_nested(d2, ['ham', 'grill'], 99))
Python nested dictionary lookup with default values