Skip to content

Instantly share code, notes, and snippets.

View atward's full-sized avatar

Adam Ward atward

  • Melbourne, Australia
View GitHub Profile
@atward
atward / safeget.py
Created May 1, 2019 05:24 — forked from WTFox/safeget.py
Perfect for handling nested dictionaries where values might not exist.
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