Skip to content

Instantly share code, notes, and snippets.

@h3
Created May 15, 2013 04:05
Show Gist options
  • Save h3/5581591 to your computer and use it in GitHub Desktop.
Save h3/5581591 to your computer and use it in GitHub Desktop.
Returns a dict from a dict list given a matching key value pair and returns None if no matches.
def get_dict_from_list(l, k, v):
"""
Returns a dict from a dict list given a matching key value pair
and returns None if no matches.
>>> test = [{'A': 'a1', 'B': 'b1'}, {'B': 'b2', 'A': 'a2', }]
>>> get_dict_from_list('A', 'a2')
{'B': 'b2', 'A': 'a2', }
"""
return next((item for item in l if item.get(k) == v), None)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment