I am trying to retrieve the item from a key in select dictionaries within a list of dictionaries, where selection of the dictionary is conditional on the value of the item against multiple other keys present in the dictionaries.
E.g. For list of dictionaries, d:
d = [{a:'2.1', z:'apple', aa:'banana'}, {a:'3.6', z:'cherry', aa:'peach'}, {a:'4.7', z:'apple', aa:'banana'}, {a:'1.6', z:'apple', aa:'orange'}]
I am interested in retrieving the item against 'a'
for the last dictionary in the list where 'z':'apple'
and 'aa':'banana'
are satisfied conditions i.e. get the item against key 'a'
from d[-2] in the above example.
Is there some simple code to do this?
I have tried:
Python: get a dict from a list based on something inside the dict - but not sure how to extend this to be conditional on the items against multiple keys inside the dictionaries.
Slicing a dictionary - but not sure how to add the conditional aspect using Python syntax.
I expect the solution involves some sort of dictionary comprehension, but am relatively new to Python.
Any help appreciated.