I am looking for a built-in/library function or method in Python that searches a list for a certain element that satisfies a predicate and returns the index. I'm looking for a built-in function because I feel that this should be in the Python libraries.
It is similar to, e.g. [(10, 1), (20, 2), (30, 3)].index((30, 3))
that returns 2, but I want a custom comparison function, for example, I just want to match the first element of the tuple in the earlier example.
What I am looking is a function that essentially does this:
def custom_index(l, f):
for i, e in enumerate(l):
if f(e): return i
return -1 # or something else to indicate not found
Usage:
custom_index([(10, 1), (20, 2), (30, 3)], lambda x: x[0]==30) -> returns 2