I have an array of dictionary like:
[{'Key':'Name', 'Value':'Thomas'},{'Key':'Location', 'Value':'EMEA'}]
I want to retrieve the Value 'EMEA' if Key is 'Location'. How can I achieve it?
CodePudding user response:
Using list comprehension
items = [{'Key':'Name', 'Value':'Thomas'},{'Key':'Location', 'Value':'EMEA'}]
res = [item['Value'] for item in items if item['Key'] == 'Location']
print(res)
CodePudding user response:
You need to iterate over each dictionary:
x = [{'Key': 'Name', 'Value': 'Thomas'}, {'Key': 'Location', 'Value': 'EMEA'}]
for entry in x:
if entry["Key"] == "Location":
print(entry["Value"])
EMEA