A list:
a = [\'a\', \'b\', \'c\', 3, 4, \'d\', 6, 7, 8]
I want a list using a subset of a using a[0:2],a[4], a[6:]
,
t
The following definition might be more efficient than the first solution proposed
def new_list_from_intervals(original_list, *intervals):
n = sum(j - i for i, j in intervals)
new_list = [None] * n
index = 0
for i, j in intervals :
for k in range(i, j) :
new_list[index] = original_list[k]
index += 1
return new_list
then you can use it like below
new_list = new_list_from_intervals(original_list, (0,2), (4,5), (6, len(original_list)))