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

11 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.

1 Like

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.

6 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:]

in this case found del to be easier:
def remove_middle(lst, start, end):
del lst[start:end+1]
return lst

1 Like