Skip to content

Instantly share code, notes, and snippets.

@shinsaka
Created November 1, 2018 01:30
Show Gist options
  • Save shinsaka/76061074980cf769a2b341cbcf2e8a1e to your computer and use it in GitHub Desktop.
Save shinsaka/76061074980cf769a2b341cbcf2e8a1e to your computer and use it in GitHub Desktop.
Get value from list of dict that has "Key" and "Value" key.
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