Skip to content

Instantly share code, notes, and snippets.

@h3

h3/getdict.py

Created May 15, 2013
Embed
What would you like to do?
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