Created
November 1, 2018 01:30
-
-
Save shinsaka/76061074980cf769a2b341cbcf2e8a1e to your computer and use it in GitHub Desktop.
Get value from list of dict that has "Key" and "Value" key.
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 getValue(key, items): | |
""" | |
Get value from list of dict that has "Key" and "Value" key. | |
""" | |
values = [x['Value'] for x in items if 'Key' in x and 'Value' in x and x['Key'] == key] | |
return values[0] if values else None | |
# 使用例 | |
items = [{'Key': 'Name', 'Value': 'apple'}, | |
{'Key': 'Color', 'Value': 'red'}, | |
{'ey': 'Color', 'Value': 'red'}, | |
{'Key': 'Quantity', 'Value': 10}] | |
print(getValue('Name', items)) | |
# apple | |
print(getValue('hoge', items)) | |
# None | |
print(getValue('Quantity', items)) | |
# 10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment