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>Using list comprehension:</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)]

new_list = [my_list[i:j] for i,j in my_tups]
</code></pre>

<hr>

<p>After your comment:</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)]

new_list = [0 for i in my_list] # Create a list filled with zeros

for i,j in my_tups:
    new_list[i:j] = my_list[i:j] # Replace items with items from my_list using the indexes from my_tups
</code></pre>

<p>Output:</p>

<pre><code>&gt;&gt;&gt; new_list
[0, 0, 0, 0, 0, 1, 3, 4, 8, 0, 0, 0, 0, 0, 21, 34, 25, 91, 0, 0, 0]
</code></pre>

python list tuples slice