Skip to content

Instantly share code, notes, and snippets.

@JoaoCarabetta
Created February 11, 2020 20:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoaoCarabetta/c318d9299ee2cd56604387e9a1741ba6 to your computer and use it in GitHub Desktop.
Save JoaoCarabetta/c318d9299ee2cd56604387e9a1741ba6 to your computer and use it in GitHub Desktop.
Safely access key from nested dict
def accessr(d, keys, default=None):
if len(keys) and d is not None:
return accessr(d.get(keys[0], default), keys[1:], default)
else:
return d
def access(d, keys, default=None):
for k in keys:
if d is not None:
d = d.get(k, default)
else:
break
return d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment