Given this map
r = {
'items': [
{
'id': '1',
'name': 'foo'
},
{
'id': '2',
'name': 'bar'
}
]
}
I am trying to get the 'id' for 'name'=='foo'. I have this:
Id = [api['id'] for api in r['items'] if 'foo' in api['name']]
But then Id == ['1']
. I want it to = "1". I can do this:
Id = [api['id'] for api in r['items'] if 'foo' in api['name']][0]
But that seems like a workaround. Is there a way to write that in such a way as to pass only the value of api['id']
rather than the value within a list?