def lookup(dic, key, *keys):
    if keys:
        return lookup(dic.get(key, {}), *keys)
return dic.get(key)
d = {'a': {'b': {'c': 5}}}
print(lookup(d, 'a', 'b', 'c'))
print(lookup(d, 'a', 'c'))
Python - accessing values nested within dictionaries