Why not just use `remove()`?

Question

Can’t I just use remove()? Why is that not a good solution?
From: FAQ: Code Challenge: Lists - Remove Middle

Answer

To start: documentation on remove:
"list.remove(x) - Remove the first item from the list whose value is x . It is an error if there is no such item."
remove() is based around removing an item based on its value, which is great if you have the value but a bad choice if you do not have the value. In this exercise you want to be making use of the information provided in your function, that is the index value, to solve it.

1 Like

Man, that +1 made me feel really dumb, because it looks simple now. I search for a .remove function on Google and used it do solution:

def remove_middle (lst, start, end):
new_lst = lst[:start] + lst[end:]
new_lst.remove(lst[end])
return new_lst

4 Likes

I knew about the .remove, however I don’t recall codecademy teaching us .remove

12 Likes

I found that to happen during the lessons, where the solution contains a way you haven’t learned yet. In a future lesson they will tell you to do an input and you haven’t learned it yet, so you just look at the answer and learn it that way.

I normally do it myself and then reset and take a look at the solutions every time. I try to see if they did it differently and if there something I could learn or how I could have done it better.

4 Likes

If the method hasn’t been taught yet, then don’t use it. Learn to work with what you have been taught up to the point and stop complaining about the course. The .remove method is not the solution for this problem so don’t even bother with it.

9 Likes

#Write your function here
def remove_middle(lst, start, end):
new_lst = lst[start:end+1]
for i in new_lst:
lst.remove(i)
return lst
#Uncomment the line below when your function is done
print(remove_middle([4, 8, 15, 16, 23, 42], 1, 3))

Combining techniques is kind of redundant, but it is nice to see the .remove method used correctly, for a change. Combining two slices is a more direct approach…

    return lst[:start] + lst[end + 1:]
1 Like

This is what I used based on what we have learned so far.

def remove_middle(lst, start, end):
lst[start:end + 1] = [ ]
return lst

1 Like

I don’t remember the lessons teaching us that assigning a range to an empty list would actually delete that portion. I guess intuition should have dictated but I really just learned that from reading and running your comment. The best I could do with all that we learned is:

def remove_middle(lst, start, end):
lst = lst[:start] + lst[end + 1:]
return lst

It’s actually the other way around… An empty list is being assigned to replace a range (slice) in the list.

Compare,

>>> def remove_middle(lst, start, end):
    lst = lst[:start] + lst[end + 1:]
    return lst

>>> remove_middle([4, 8, 15, 16, 23, 42], 1, 3)
[4, 23, 42]

to,

>>> def remove_middle(lst, start, end):
	lst[start:end + 1] = []
	return lst

>>> remove_middle([4, 8, 15, 16, 23, 42], 1, 3)
[4, 23, 42]
>>> 

The first method reconstructs the string with front and back slices that exclude the middle. The second mutates the middle slice. Both approaches return the same result when supplied a list.

It follows that something this general depends upon valid inputs.

  • verify that lst is indeed a list object
  • verify that the list has some length
  • verify that start and end are in range
  • verify that end is greater than start

Since the function uses slices it may not be necessary to the validate the length; let’s see…

>>> remove_middle([], 1, 3)
[]
>>> 

The first approach will also take a string.

>>> def remove_middle(obj, start, end):
    obj = obj[:start] + obj[end + 1:]
    return obj

>>> remove_middle('9876543210', 1, 3)
'9543210'
>>> 

for this one we could add,

  • verify that the object is a string (when not a list)

We should be mindful that invalid index inputs will not raise any exceptions since we’re working with slices. Invalid indices are ignored.

>>> remove_middle('9876543210', 11, 13)
'9876543210'
>>> 

That’s all the more reason to have our own validation code.

3 Likes

All i did was this. Seems to work just fine without .remove()

def remove_middle(lst,start,end):
  new_list= lst[:start] + lst[end+1:]
  return new_list

print(remove_middle([4, 8, 15, 16, 23, 42], 1, 3))

Hi all,I am not getting my head around why end + 1 is used here. I went through all explanations but still I am not getting it. I can use remove method but i want to understand the logic beding end+1. Please help.
Thank you.