Skip to content

Instantly share code, notes, and snippets.

View yaznahar's full-sized avatar

Alexander Larin yaznahar

View GitHub Profile
@yaznahar
yaznahar / key_chain.py
Last active May 22, 2023 16:26
Simple and safe way to get value of a nested dictionray
def key_chain(data, *args, default=None):
for key in args:
if isinstance(data, dict) and key in data:
data = data[key]
elif isinstance(data, (list, tuple)) and isinstance(key, int):
try:
data = data[key]
except IndexError:
return default
else: