I read that question about how to use bisect
on a list of tuples, and I used that information to answer that question. It works, but I\'d like a more generic so
For this:
...want to find the first item where x > 5 for those (x,y) tuples (not considering y at all)
Something like:
import bisect
test_array = [(1,2),(3,4),(5,6),(5,7000),(7,8),(9,10)]
first_elem = [elem[0] for elem in test_array]
print(bisect.bisect_right(first_elem, 5))
The bisect_right function will take the first index past, and since you're just concerned with the first element of the tuple, this part seems straight forward. ...still not generalising to a specific key function I realize.
As @Jean-FrançoisFabre pointed out, we're already processing the entire array, so using bisect may not even be very helpful.
Not sure if it's any quicker, but we could alternatively use something like itertools (yes, this is a bit ugly):
import itertools
test_array = [(1,2),(3,4),(5,6),(5,7000),(7,8),(9,10)]
print(itertools.ifilter(
lambda tp: tp[1][0]>5,
((ix, num) for ix, num in enumerate(test_array))).next()[0]
)