I am trying to remove some items from a list. Those items to be removed are defined by different ranges given by two lists that define the start and end of the ranges.
The inputs shall have the following structure but let me point out that the first & last list shall be flexible, not always their lengths are going to be 2:
list = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
first = [3,9]
last = [5,13]
From my initial list, I need to remove items between values 3 & 5 and items between values 9 & 13 so my final outcome would be:
lst = [0,1,2,6,7,8,14,15]
Let me post what I´ve done so far with no success:
list = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
first = [3,9]
last = [5,13]
lst = []
for i in list:
for j, k in zip(first, last):
if i > j and i < k:
break
else:
lst.append(i)
break
print(lst)
Thank you and regards,
jis a minValue andka maxValue. Then when you enter the forbidden regioni > j and i < kyou break the loop. You should not break the loop. Otherwise you will never append the values greater thanmaxValsince that loop will have already been broken. I would recommend you run a debugger line by line, and you will see why it doesn;t work.firstandlastalways come in ascending order?