I have data in the form of a list of dicts, with some keys and values (the example below is for two keys/values, there could be more):
I would like to pythonically answer the question: what is john
's age
?
The simple solution I am using now is to iterate though the list and check for a matching value of name. Is there a more pythonic way to achieve the same?
data = [
{
'name': 'john',
'age': 10
},
{
'name': 'paul',
'age': 20
}
]
age = -1 # in case the name is not found
for d in data:
if d['name'] == 'john':
age = d['age']
print(age)