title: Finding the index of an element that satisfies a predicate in Python
body: <p>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.</p>
  
  <p>It is similar to, e.g. <code>[(10, 1), (20, 2), (30, 3)].index((30, 3))</code> 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.</p>
  
  <p>What I am looking is a function that essentially does this:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Usage:</p>
  
  <pre><code>custom_index([(10, 1), (20, 2), (30, 3)], lambda x: x[0]==30) -&gt; returns 2
  </code></pre>