# Code Challenges: Lists (Remove Middle)

Hi guys, it is me again…
I got so stuck in this list, it is asking me remove the 1st, 2nd and 3rd index. But I had to look up the answer because I have no clue of how to do it.
I am copying the code here:

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

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

what `range` here:

``````lst[:range]
``````

`range` variable is undefined. What should be used here? What are you trying to do here?

2 Likes

Think about the indicies of the numbers in the list. (remember the index starts at 0.)
The function takes three parameters, `lst, start, end`

The instructions say "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)`

1 Like

Instead of `range` it was:
`lst[:start]`

So then it prints the desired list, but I still don’t get how those lines of code get me to that answer:

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

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

This does get me to the right answer but how does that return line work?

Results: `[4, 23, 42]`

When you call the function, you’re passing through 3 arguments: a list (lst), starting point and end point.

`[:start]` will give you the item at the beginning of the list to the stop point. (the stop point as indicated in the function call is 1), which is the number 8, so, the function returns 4.

`[end+1:]` will give you the end of the list (whatever the number is) plus 1 (ie: moving to the right). The end here is index 3, which is the number 16 in the list, +1 to that is 23.
You’ve removed the elements at indicies 1, 2, & 3 or, the middle of the list.

Pass through different lists of numbers and starting and end points and see what happens. change it up so you understand it better.

Ex: what happens if I pass this through? What is returned?

`print(remove_middle([99, 8, 27, 15, 12, 1, 44], 1, 4))`

4 Likes

Thanks for all of your help! Omg I feel like this took me too long to understand, but now I see it!
So that will return

``````[99, 1, 44]
``````

Am I right?

2 Likes

You’re welcome.
Correct!

Functions can be difficult. But, if you break it down into digestible pieces and in words that make sense to you, and maybe even talking it out loud, you’ll get it.