Skip to content

Instantly share code, notes, and snippets.

Avatar

Adam Ward atward

  • Melbourne, Australia
View GitHub Profile
@atward
atward / safeget.py
Created May 1, 2019 — forked from WTFox/safeget.py
Perfect for handling nested dictionaries where values might not exist.
View safeget.py
def safeget(dct, *keys):
dct = dict(dct)
for key in keys:
try:
dct = dct[key]
except (KeyError, AttributeError, TypeError) as e:
return None
return dct