Searching through a list of tuples

Scott David Daniels Scott.Daniels at Acm.Org
Tue Jul 12 16:15:14 EDT 2005


Peter Otten wrote:
> Repton wrote:
> 
>>I often find myself storing data in a list of tuples, and I want to ask
>>questions like "what is the index of the first tuple whose 3rd element
>>is x", 
     iter(n for n, elem in enumerate(lst) if elem[3] == x).next()

>>or "give me the first tuple whose 2nd element is y".
     iter(elem in lst if elem[3] == x).next()

Does this look any better?  At least it stops when the answer is found.

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list