Created
May 15, 2013 04:05
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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