1

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,

4
  • could you name your variables with more descriptive names? for what I deduce in your code j is a minValue and k a maxValue. Then when you enter the forbidden region i > j and i < k you break the loop. You should not break the loop. Otherwise you will never append the values greater than maxVal since 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. Commented Sep 21, 2022 at 10:13
  • 1
    Do numbers in first and last always come in ascending order? Commented Sep 21, 2022 at 10:13
  • 2
    Do you want to remove the items in those ranges, or the items at the indices indicated by the ranges? Both are the same in your example. Commented Sep 21, 2022 at 10:19
  • the values within the first = [] and last = [] list do not mean the indices where I need to remove items from. I just want to remove items in those ranges. Commented Sep 21, 2022 at 10:32

2 Answers 2

2

You can build the range objects, for all the corresponding elements from first and last, like this

>>> ranges = [range(start, end + 1) for start, end in zip(first, last)]

then just check if the current number does not fall under any of the ranges. This is possible as the range objects allow us to query if a particular number is within the range or not.

>>> [num for num in list if all(num not in rng for rng in ranges)]
[0, 1, 2, 6, 7, 8, 14, 15]

In your code, you have to check all the ranges to see if the current number is not there in any of them. Only if the current number satisfies that, it should be included in the result.

list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
first = [3, 9]
last = [5, 13]
result = []


for current_num in list:
     found_matching_range = False

     for start, end in zip(first, last):
          if start <= current_num <= end: # Note the `=`. Inclusive comparison
               found_matching_range = True
               break

     if not found_matching_range:
          result.append(current_num)
print(result)
Sign up to request clarification or add additional context in comments.

Comments

1

You almost had it. Just unindent the else to where it belongs, remove the bad break, and make the comparisons inclusive:

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)
print(lst)

Try it online!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.