Advance Python List Challenges: Exercise 2 - Remove Middle

I was trying to do Exercise 2 of the Advance List challenges, but when I looked at the Solution provided, it made no sense to me. I need help understanding why is the solution written that particular way. As per the explanations on the list section of the Python3 cheat sheet, the solution to this exercise makes no sense. Thank you in advance

1 Like

What is the given solution? Something like this…

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



Yep, something like that. I just don’t understand how that particular code would work. I would have never come up with that particular solution on my own.

That approach uses a technique called list slicing. A slice is a subscript that contains a colon, (:). It is the simplest in terms of code, but if we have not yet encountered slices in any lessons, then it would be good to see if you can find a solution using only what you know. Looping and indexing, perhaps.

Earlier today we explored one such example…

FAQ: Code Challenge: Lists - Remove Middle

You could take the time to explore list slicing, but I would still encourage you to come up with other possible solutions using what you know.

1 Like

You’re given a list. Forget code for a moment and ask yourself what you would need to do with a list to get what you want. Then take a look at what operations a list supports and see if there’s something among that matching what you need, or if there are things you can use together to make what you need.

1 Like

Thank you mtf, this helps!

1 Like

I was also thinking the same thing. Why wouldn’t you just use the .pop[-2] and so forth to get the middle out.

1 Like

Is the intention to modify the original or to obtain a result where the middle is missing?

If it’s the former then you have no reason to return a result.
If it’s the latter then modifying the original is a side-effect which is likely unwanted, suddenly your function is doing two things instead of one, what if the caller only wants one of them? That function is now difficult to use correctly.

1 Like

obtain a result.
I am not clear on how slicing works in this exercise especially the end+1 in an array

1 Like

What do you mean by works?
Start with what it at all does
and then consider what information is involved
and what it promises to do with information you provide
and then if you’re wondering whether or not you’d need to send in some other value than you already happen to have, well, is what you have what’s needed, or not?

Asking why you need +1 seems to me like being too concerned with the written text in the code, and forgetting what actions are actually going on.

1 Like

ok, I think that helps. I went from the solution to the root to figure it out.

1 Like

Now you have me confused. Please explain. Thanks.

1 Like

Oh hi mtf,
Sorry for getting back so late.
From some of the walk-throughs in the last exercises, they suggested you work backwards after getting the solution. I tried to make some sense of it by going backwards am not 100% sure what it all means. I was trying to make some sense of what the other guy was suggesting. Python is hard to figure out at the end with strings, list, and loops. Some of the questions are hard to comprehend.

1 Like

It’s called reverse engineering. Take a finished piece of code and analyze from the bottom to the top, from the last step to the first.

First look at the correct solution, and the inputs it is derived from.

[4, 16, 23, 42]

[4, 8, 15, 16, 23, 42], 1, 3

Notice that 8 and 15 are removed in the solution. It just happens they are bounded within the list by index 1, inclusive, and index 3, exclusive.

In the drawn out approach (link in post 3, above) we see how the right side, beginning at index 3 inclusive, and to the end, is appended to the left side, from index 0, up to but exclusive of index 1.

 [4] + [16, 23, 42]

This relates exactly to the inputs so we can have some confidence in the program and understand how it works. Of course that means we need to fully understand how range() works, and how.append() behaves with respect to list objects.

Study the problem backwards and forwards until you can clear the slate and write the program not from memory or rote, but from your understanding of the concepts, the algorithm, the inputs and obviously lists and indices. Have fun with code and learn it inside out. No time invested this way will result in nothing but confusion down the road.

1 Like

ok I think I got mtf. (1rst, start, end) when used in a function type in this case will equal
1rst[start = being when indices starts in the array] + [end = being when the indices ends in the arry]

1 Like

Be careful to never write a variable name with a numeral as the first character. They are not permitted. Always start with a letter or underscore (for special cases).

Above we kept all values from index 0 up to by not including start. We also kept all the values from index end through to the end of the list.

Essentially, if we use a slice, we have,

lst[:start] + lst[end:]
1 Like

Hello, I saw this thread relating to the topic, I wanted to ask for clarity when you said this?

What exactly does this mean?

Inclusive and exclusive refer to terms that will be in the final range to be considered. The start index of a range is inclusive. That is the index we start on. However the end index of a range is exclusive. If the end index of a range is 10, then the range will exclude it, and end on 9.

print (list(range(10)))

#  [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]

Huh, I sort of understand what you are saying, I’m just confused by what you meant that index 1 and index 3 is bounded to 8 through 15.

Does index 3 starts at 8 and then end on index 1 at 15?

Oh wait I think I’m starting to get it, thank you for replying by the way!

1 Like