using bisect on list of tuples but compare using first value only
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 solution. Since bisect doesn't allow to specify a key function, if I have this: import bisect test_array = [(1,2),(3,4),(5,6),(5,7000),(7,8),(9,10)] and I want to find the first item where x > 5 for those (x,y) tuples (not considering y at all, I'm currently doing this: bisect.bisect_left(test_array,(5,10000)) and I get the correct result because I know that no y is greater than 10000, so bisect points me to the index of (7,8) . Had I put