I would write a custom indexer function as follows:
def safe_indexer(obj, *indices):
for idx in indices:
if not obj: break
if hasattr(obj, "get"):
obj = obj.get(idx)
else:
obj = obj[idx]
return obj
Using:
a = {"key": {0: {0: "foo"} } };
print safe_indexer(a, "key", 0, 0)
print safe_indexer(a, "bad", 0, 0)
Output:
foo
None