How to find the indices of a subset in a list?
<p>I have a list of values that I know are increasing, such as</p>

<pre><code>x = [1, 2, 3, 4, 5, 6]
</code></pre>

<p>I'm looking for the indices of the subset that are within some range <code>[min, max]</code>. E.g. I want</p>

<pre><code>&gt;&gt; subset_indices(x, 2, 4)
[1, 3]
&gt;&gt; subset_indices(x, 1.1, 7)
[1, 5]
</code></pre>

<p>Is there a nice pythonic way of doing this?</p>

<p>Following the recommendations from Kenny Ostrom and volcano, I implemented it simply as</p>

<pre><code>import bisect

def subset_indices(sequence, minv, maxv):
    low = bisect.bisect_left(sequence, minv)
    high = bisect.bisect_left(sequence, maxv, lo=low)
    return [low, high]
</code></pre>

python list subset