Removing from List

As per below scenario and solution, I don’t understand the following code:
return lst[:start] + lst[end+1:]
how does this return [4, 23, 42]

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

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

For example, the following code should return [4, 23, 42] because elements at indices 1 , 2 , and 3 have been removed:

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

def remove_middle(lst, start, end):
return lst[:start] + lst[end+1:]
print(remove_middle([4, 8, 15, 16, 23, 42], 1, 3))

Please post a link to this exercise.

My apology, I forgot.

Here is more details:
Topic: 2. Remove Middle
From: Advanced Python Code Challenges: Lists
Difficult Python Code Challenges involving Lists


1 Like

Is that your solution, or the one given as the Solution?

Given solution.

Have you learned about list and string slicing, yet?

Yes. I’m well aware.
I just cant wrap my head around this slicing:
return lst[:start] + lst[end+1:]
This was not taught in the exercise or maybe I did not grasp it.

Looking at the list, and the indices,

 0  1   2   3   4   5
[4, 8, 15, 16, 23, 42]
    1   2   3          <= to be removed

Since end is 3, we need to to add 1 for the first element in the right side of the returned list.

Consider this rather verbose demonstration that does not use slicing, but instead uses enumerate(),

return [x for i, x in enumerate(a) if i not in range(1, 4)]

List slicing works similar to range in that the last index is excluded.

Here’s another take of the slice approach…

a[start:end+1] = []

Hope this not a dumb question.
As per these parameters:
def remove_middle(lst, start, end):

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

Does it mean that
start = 8 (1st index of the above list)
end = 16 (3rd index of the above list)

Yes, since the instructions specify to include the end index in the removed elements.


Because a slice works like range(), the value at index 1 is not included in that slice.


Because end is not to be included in the returned list, we must add 1.

1 Like

I’m having the same issue as this wasn’t covered in the exercises.

I looked up the exercises TWICE. This solution was NOT covered in the exercises.
What is the point of the challenge if we are not equipped to solve it? :roll_eyes:

Hey there @stanmarsh01 :grinning: Welcome to the Codecademy forums!

As long as you had learned about list indexing and functions, than the course has given you what you need to solve the lesson.

I believe the point of the challenge is so you can learn to experiment with your ‘tools’. You will gain experiance with it and better problem solving skills. A program developer is only told what is needed, not how to get there. It is up to the programmer to solve this.

Challenges like this build experiance, and help you learn to use your coding knowledge.
It is the same reason why websites like code wars offer small challenges.

1 Like