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