How to select all values from one threshold to another?
<p>I have an ordered tuple of data:</p>

<pre><code>my_data = (1,2,3,2,4,2,3,3,5,7,5,3,6,8,7)
</code></pre>

<p>How can I subset the tuple items such that all items including and after the first instance of 3 are kept until the first value == 7? For example, the result should be:</p>

<pre><code>desired_output = (3,2,4,2,3,3,5,7)
</code></pre>

<p><code>itertools</code> has <code>dropwhile</code> and <code>takewhile</code> that you can use to your advantage. Use <code>dropwhile</code> to drop everything until the first <code>3</code>, and <code>takewhile</code> to take everything until the first 7 thereafter. Then just merge the two</p>

<pre><code>import itertools

L = [1,2,3,2,4,2,3,3,5,7,5,3,6,8,7]
answer = list(itertools.takewhile(lambda x: x!=7, itertools.dropwhile(lambda x: x!=3, L)))
answer.append(7)

In [49]: answer
Out[49]: [3, 2, 4, 2, 3, 3, 5, 7]
</code></pre>

python tuples