Given your data structure:
>>> [item for item in accounts if item.get('id')==2]
[{'title': 'Gow to get this one?', 'id': 2}]
If item does not exist:
>>> [item for item in accounts if item.get('id')==10]
[]
That being said, if you have opportunity to do so, you might rethink your datastucture:
accounts = {
1: {
'title': 'Example Account 1'
},
2: {
'title': 'Gow to get this one?'
},
3: {
'title': 'Example Account 3'
}
}
You might then be able to access you data directly by indexing their id
or using get()
depending how you want to deal with non-existent keys.
>>> accounts[2]
{'title': 'Gow to get this one?'}
>>> accounts[10]
Traceback (most recent call last):
File "<input>", line 1, in <module>
KeyError: 10
>>> accounts.get(2)
{'title': 'Gow to get this one?'}
>>> accounts.get(10)
# None