Q:

Python Slice a List Using a List of Multiple Tuples

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:
my_list = [ 5, 8, 3, 0, 0, 1, 3, 4, 8, 13, 0, 0, 0, 0, 21, 34, 25, 91, 61, 0, 0,] 

I also have a list of tuples that are the indicies of values that that I want that looks like:
my_tups = [(5,9), (14,18)]

How would I return only the values of my_list using my_tups as an index?     

A:

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

After your comment:
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

Output:
>>> new_list
[0, 0, 0, 0, 0, 1, 3, 4, 8, 0, 0, 0, 0, 0, 21, 34, 25, 91, 0, 0, 0]

