find a specified dictionary in a list

John Machin sjmachin at lexicon.net
Fri Jul 22 08:30:01 EDT 2005


John Machin wrote:
> Odd-R. wrote:
> 
>> I have this list:
>>
>> [{'i': 'milk', 'oid': 1}, {'i': 'butter', 'oid': 2},{'i':'cake','oid':3}]
>>
>> All the dictionaries of this list are of the same form, and all the oids
>> are distinct. If I have an oid and the list, how is the simplest way of
>> getting the dictionary that holds this oid?
>>
> 
> Something like this:
> 
> def oidfinder(an_oid, the_list):
>     for d in the_list:
>     if d['oid'] == an_oid:
Better indented as:
 >         if d['oid'] == an_oid:
>             return d
>     return None
>     # These are not the oids you are looking for.



More information about the Python-list mailing list