Python Slice a List Using a List of Multiple Tuples
<p>I have a list of numbers that I would like to slice the range of numbers that is given from a list of multiple tuples. For example, I have a list that looks like:</p>

<pre><code>my_list = [ 5, 8, 3, 0, 0, 1, 3, 4, 8, 13, 0, 0, 0, 0, 21, 34, 25, 91, 61, 0, 0,] 
</code></pre>

<p>I also have a list of tuples that are the indicies of values that that I want that looks like:</p>

<pre><code>my_tups = [(5,9), (14,18)]
</code></pre>

<p>How would I return only the values of my_list using my_tups as an index?    </p>

<p>If I understand the question correctly, you want to return the values from <code>my_list</code> in the ranges <code>5:9</code> and <code>14:18</code>. The following code should do it</p>

<pre><code>my_list = [ 5, 8, 3, 0, 0, 1, 3, 4, 8, 13, 0, 0, 0, 0, 21, 34, 25, 91, 61, 0, 0]
my_tups = [(5,9), (14,18)]

def flattens(lists):
    return sum(lists, [])
flatten([my_list[lo:hi] for (lo, hi) in my_tups])
# gives [1, 3, 4, 8, 21, 34, 25, 91]
</code></pre>

python list tuples slice