Skip to content

Instantly share code, notes, and snippets.

@neilsh
Forked from douglasmiranda/gist:5127251
Last active December 8, 2016 02:09
Show Gist options
  • Save neilsh/febd32c8284b1dd81ea61495dce60c0a to your computer and use it in GitHub Desktop.
Save neilsh/febd32c8284b1dd81ea61495dce60c0a to your computer and use it in GitHub Desktop.
def find(key, dictionary):
if isinstance(dictionary, list):
for item in dictionary:
for result in find(key, item):
yield result
elif isinstance(dictionary, dict):
for k, v in dictionary.iteritems():
if k == key:
yield v
for result in find(key, v):
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))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment