Check if values in list exceed threshold a certain amount of times and return index of first exceedance
<p>I am searching for a clean and pythonic way of checking if the contents of a list are greater than a given number (first threshold) for a certain number of times (second threshold). If both statements are true, I want to return the index of the first value which exceeds the given threshold.</p>

<p><strong>Example</strong>:</p>

<pre><code># Set first and second threshold
thr1 = 4
thr2 = 5

# Example 1: Both thresholds exceeded, looking for index (3)
list1 = [1, 1, 1, 5, 1, 6, 7, 3, 6, 8]

# Example 2: Only threshold 1 is exceeded, no index return needed
list2 = [1, 1, 6, 1, 1, 1, 2, 1, 1, 1]
</code></pre>

<p>Try this:</p>

<pre><code>def check_list(testlist)
    overages = [x for x in testlist if x &gt; thr1]
    if len(overages) &gt;= thr2:
        return testlist.index(overages[0])
    # This return is not needed. Removing it will not change
    # the outcome of the function.
    return None
</code></pre>

<p>This uses the fact that you can use if statements in list comprehensions to ignore non-important values.</p>

<p>As mentioned by Chris_Rands in the comments, the <code>return None</code> is unnecessary. Removing it will not change the result of the function.</p>

python list iterator threshold