Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@lsloan
Forked from douglasmiranda/gist:5127251
Last active December 5, 2018 21:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lsloan/2d9e88ecf468c0f484d8d18cefe27aeb to your computer and use it in GitHub Desktop.
Save lsloan/2d9e88ecf468c0f484d8d18cefe27aeb to your computer and use it in GitHub Desktop.
def find(key, dictionary):
for k, v in dictionary.items():
if k == key:
yield v
elif isinstance(v, dict):
for result in find(key, v):
yield result
elif isinstance(v, list):
for d in v:
for result in find(key, d):
yield result
example = {'app_url': '', 'models': [{'perms': {'add': True, 'change': True, 'delete': True}, 'add_url': '/admin/cms/news/add/', 'admin_url': '/admin/cms/news/', 'name': ''}], 'has_module_perms': True, 'name': u'CMS'}
list(find('admin_url', example))
@lsloan
Copy link
Author

lsloan commented Dec 5, 2018

I updated this to work with Python 3 by changing line 2 to use items().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment