Re: Searching through a list of tuples

2005-07-13 Thread Peter Otten
Scott David Daniels wrote: iter(elem in lst if elem[3] == x).next() Does this look any better?  At least it stops when the answer is found. Next time you'll recommend if (ab) == True: # ... Watch out, you're on a slippery slope here :-) Peter --

Re: Searching through a list of tuples

2005-07-12 Thread Scott David Daniels
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

Re: Searching through a list of tuples

2005-07-12 Thread Alan Green
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, or give me the first tuple whose 2nd element is y. items = [(1, a, 10), (2, b, 20), (3, c, 30)] class

Searching through a list of tuples

2005-07-11 Thread Repton
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, or give me the first tuple whose 2nd element is y. I know I can do [elem for elem in lst if elem[3] == x][0] or (elem for elem in lst if elem[2] ==

Re: Searching through a list of tuples

2005-07-11 Thread Peter Hansen
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, or give me the first tuple whose 2nd element is y. I know I can do [elem for elem in lst if elem[3] == x][0] or (elem for elem in

Re: Searching through a list of tuples

2005-07-11 Thread Peter Otten
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, or give me the first tuple whose 2nd element is y. I know I can do [elem for elem in lst if elem[3] == x][0] or (elem for elem