Вот решение для работы с вложенными словарями:
def get(root, *keys):
"""
Returns root[k_1][k_2]...[k_n] if all k_1, ..., k_n are valid keys/indices.
Returns None otherwise
"""
if not keys:
return root
if keys[0] not in root:
return None
if keys[0] in root:
return get(root[keys[0]], *keys[1:])
Использование:
>>> d = {'a': 1, 'b': {'c': 3}}
>>> get(d, 'b', 'c')
3
>>> get(d. 'key that's not in d')
None
>>> get(d)
{'a': 1, 'b': {'c': 3}}