efficient matching of elements a list

Gerrit Holl gerrit at nl.linux.org
Sat Jan 24 05:07:25 EST 2004


> What is the fastest way in python to get out all the tuples from the 
> list whose first element  is equal to i?

Use a list comprehension:
>>> L = [(1,2,3), (2,3,1), (3,2,1), (1,2,5), (5,1,3)]
>>> [t for t in L if t[0] == 1]
[(1, 2, 3), (1, 2, 5)]

yours,
Gerrit.




More information about the Python-list mailing list