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