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>You could also use <code>slice</code> with <a href="https://docs.python.org/2/library/itertools.html#itertools.starmap" rel="nofollow"><code>itertools.starmap</code></a>:</p>

<pre><code>from itertools import starmap

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)]

result = [my_list[slc] for slc in starmap(slice, my_tups)]
# [[1.0, 3.0, 4.0, 8.0], [21.0, 34.0, 25.0, 91.0]]
</code></pre>

<hr>

<p>With <code>starmap</code>, you could add a <em>step</em> parameter to <em>any</em> of your slices arbitrarily:</p>

<pre><code>&gt;&gt;&gt; my_tups = [(5,9,2), (14,18)] # step first slice by 2
&gt;&gt;&gt; [my_list[slc] for slc in starmap(slice, my_tups)]
[[1.0, 4.0], [21.0, 34.0, 25.0, 91.0]]
</code></pre>

python list tuples slice