FAQ: Code Challenge: Lists - Remove Middle

ah okay i see i think.
to remove 8 we would do [:z - 1]?

Let’s try without adding or subtracting. Recall we want to remove the middle. How many will be removed? Answer, z - a, or 8 - 2 which is, 6.

>>> lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
>>> lst[:a] + lst[z:]
[1, 2, 9, 0]
>>> 

I guess that leaves both of us with the same question. Why end + 1? What do the instructions say? Are we to also remove the element at the second index given in the argument? If so, then that changes the scenario from above.

Consider,

    lst = [4, 8, 15, 16, 23, 42]
    #         1   2   3
    start = 1
    end = 3

3 - 1 is only 2. We need to remove 3. So,

>>> lst = [4, 8, 15, 16, 23, 42]
>>> start = 1
>>> end = 3
>>> lst[:start] + lst[end + 1:]
[4, 23, 42]
>>> 

Hope that answers your question. Be sure to review the instructions, and while you’re at it, please post a link to the exercise.

Thanks for all the help! I was able to understand what i was actually doing.
lst[:start] + lst[end+1:] = lst[:1] + lst[3+1:]

and lst[:start] selects up to, but not including index 1 ( 8 ), capturing 4. and then lst[3+1:] selects up to 16, then shifts over 1 to 23, capturing 23,42.
then we add em together then boom
4,23,42.

heres the link to the exercise: https://www.codecademy.com/paths/computer-science/tracks/cspath-flow-data-iteration/modules/dspath-lists/articles/advanced-python-code-challenges-lists

1 Like

You’re welcome, @tag3678980116.

I’m guessing you see the difference between the first example, and this exercise? We can make all manner of mistakes when dynamically coding slices so be sure to get as much practice as you can, going forward. We still like to see solutions that don’t use slicing, so give that a try, as well. See how many different solutions you can come up with over the Easter weekend.

ok, after reviewing the answers and tips, I finally came to the solution and how it works. I thought the new array should have been [:start] to [:end +1], but instead it took those out and the array actually started with the numbers that weren’t in that return list. Not sure if I’m confusing myself.

[:start]  =>  everything up to but not including index `start`

[end:]    => everything including index `end` through to the end

Out of genuine curiosity, why is this method not considered smart? If the ‘del’ function exists, is it not ok to take advantage of it if it yields the desired/same outcome?

I might be mistaken but I don’t think the particular knowledge to be able to achieve this challenge was teach prior to the challenge and it’s not the first time I notice that.
It’s a bit frustrating confidence-wise when learning something new for what it’s worth

Here was my simple solution using slice()

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

I tried this mtf, but it returned [4, 23, 42]. Only after I added the +1 did the code work. Why is that?

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

#Uncomment the line below when your function is done
print(remove_middle([4, 8, 15, 16, 23, 42], 1, 3))

Will have to review the instructions to determine if the index 3 is included or excluded. If included then yes, +1 will be needed so the value at that index is also removed.

My example above excludes that index.

this doesnt make any sense. why does start pull out the 4 and why does end pull out the 16, 23, 42 and save them into a lst? Please, please please help

This kind of bratty answer really makes one shine :poop::grin:
How to pretend to want to help others while just seizing the opportunity to be an A**
If Google is your friend then you don’t need any other site to learn right?
No, you shouldn’t have to ask questions, cause the role of a good educator is to make your learning experience smoother not harder.

here is my code ,

def remove_middle(lst, start, end):

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

you’re not - i just cannot figure out how it works

Welcome to the forums!

What’s the issue you are having? Please post any relevant code and format it using the </> button.

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

Could someone explain to me why this doesn’t work as a solution? It removes the 8 at index 1, but not the values at indices 2 and 3.

Are we to understand that when a length is odd, remove the middle one, otherwise remove the middle two?


Belay, d’oh! It looks like removing everything between A and B. Are start and end both excluded from the middle?

If so, then we would grab everything from zero to start, and concatenate it with everything from end to the end. We would need to alter this accordingly if they are included in the middle.

Given that this is a list, there is a really simple way, but I am going to hold out until we hear back from you.

Hi, thanks for replying. I have read the preferred solution, as well as others in the thread. But for completeness’s sake I’m trying to understand why this code, which was my first attempt, gave the output that it did. Looking again I’m thinking it must have something to with the indices getting messed up as soon as you start popping items out of the list, though I would still expect a different result.

Your suspicions are correct. We get one stab at the original list. If that means more than one step, then we need to keep the list intact. .pop() runs completely counter to that.

How have you progressed, so far. What new solution do you have on the drawing board?

Remember, each expression is a step.

A + B

where A and B are both expressions, means A cannot do anything to change what B has to work with since they both draw on the same data. It’s two steps because we have to evaluate twice, and on either side of the operator which is a distinct separation of concerns.