efficient matching of elements a list
Josiah Carlson
jcarlson at nospam.uci.edu
Sat Jan 24 04:12:47 EST 2004
>>Suppose I have a lists of tuples
>>A_LIST=[(1,6,7),(7,4,2),(7,9,2),(1,5,5),(1,1,1)]
>>and an int
>>i=1
>>What is the fastest way in python to get out all the tuples from the
>>list whose first element is equal to i?
>
>
> t = [x for x in A_LIST if x[0] == i]
Before list comprehensions, we had to do it all with filter...
t = filter(lambda x:x[0]==i, A_LIST)
List comprehension seem to be around 33% faster. Long live list
comprehensions.
- Josiah
More information about the Python-list
mailing list