Python remove items from list between intervals

Hi all,

I am trying to remove items from a list and the items to be removed shall fulfill a specific range given by other lists.

Let me post a screenshot:


Final outcome shall be:


As you may notice, I need to erase from my initial list the values between 3 & 5 and also between 9 & 13.

This is what I did so far but with no success:

for i in list:
  for j, k in zip(first, last):
    if i > j and i < k:

Best regards,

maybe this? (ironpython)

list = range(16)
first = [3,9]
last = [5,13]

for l in range(2):
    list = filter(lambda i: i < first[l] or i > last[l], list)

OUT = list
1 Like

You´re close but my workflow must work with flexible first & last list length instead of length = 2.

what do you mean? any example?

first and last list can have this structure:

list = range(16)
first = [3,7,12]
last = [5,9,14]

The length of both ranges cannot be fixed, it must be flexible.

try change this line:
for l in range(2):
into this?
for I in range(len(first)):

1 Like