Using del to remove elements in a list

I’m a bit confused on why my function isn’t working. I’d love any explanation. Thanks

Create a function named remove_middle which has three parameters named my_list, start, and end.

The function should return a list where all elements in my_list with an index between start and end (inclusive) have been removed.

My code:

def remove_middle(my_list, start, end):
for num in my_list:
if (num > start) and (num < end):
del num
return my_list

When I call the function, it prints out the whole list. Help please

def remove_middle(my_list, start, end):
 new_list = []
 for i in range(0, len(my_list),1):
        if (i >= start) & (i <= end):
 return new_list
  • make an empty list to add the my_list items
  • loop with for/while till the length of my_list
    -put a condition, to check the index is in between start and end. If the condition true skip the index and if false add the item to the new_list
  • return the list

Hi py6633228696! I hope your coding journey it’s getting better and better.

To complement the answer above, I want to point you some things.

  1. Regarding you code, it appears you give as input a list and then you give two integers (referring to two indices), so you have an object type: list and two object types: integer
  2. When you iterate through list ( for num in my_list:) you are iterating through the items of the list, not the list indexes, that’s why you can’t compare num (an item of your list). Think if your list is made of words, instead of numbers, you can’t compare words and number

Here is my implementation:

def remove_middle(my_list, start, end):
    returned_list = []
    for i in range(0, len(my_list)):
        if i < start:
        elif i > end:
    return returned_list
num_list = [2, 3, 5, 8, 0, 92, 9]
fruits_list = ["apple", "banana", "mangoe", "grapes"]

print(remove_middle(num_list, 2, 4))
# prints [2, 3, 92, 9]
print(remove_middle(fruits_list, 2, 3))
# prints ['apple', 'banana']

The explanation goes:

  1. We iterate through the indices creating a range between zero and lenght of the list, that way we can compare integers to integers
  2. We compare the index we are evaluating to the start and end indixes provided.
  3. We returned the item values with indexes not included between start and end.

I hope that helps!


thanks for your help!

1 Like

thanks for the explanation, that really helps

The elephant in the room, here, is that none of the responses dealt with the question, only provided alternate code. That is not much help, imho.

Now to the actual question, let one ask another, preliminary question:

What happens to the list after we del an item; and, how does that relate to the current iteration pointer?

We need to give this some thought. I’ll wait for your reply.

While I’m waiting, here is some preliminary reading:

5. Data Structures — Python 3.11.3 documentation

Also, consider that we are not deleting an item, but deleting the reference to it. Consider this recursive function:

def remove_between(my_list, start, end):
    for num in my_list:
        if num > start and num < end:
            del my_list[my_list.index(num)]
            return remove_between(my_list, start, end)
    return my_list
>>> print (remove_between([3, 4, 5, 6, 7, 8, 9, 10, 11, 12], 5, 10))
[3, 4, 5, 10, 11, 12]
>>> from random import randint as rand
>>> y = [rand(1, 100) for x in range(10)]
>>> print (sorted(remove_between(y, 25, 75)))
[13, 17, 80, 81, 84, 97]
def mix(a, b, c=10):
    return [rand(a, b) for _ in range(c)]
>>> sorted(remove_between(mix(1, 100), 25, 75))
[2, 5, 7, 83, 83, 96]
>>> sorted(remove_between(mix(1, 100), 25, 75))
[13, 14, 15, 25, 92]
>>> sorted(remove_between(mix(1, 100), 25, 75))
[4, 5, 12, 75, 78, 100]
>>> sorted(remove_between(mix(1, 100), 25, 75))
[16, 76, 96]

I’ll explain more when you get back to us.