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
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…
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.
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.
Thank you mtf, this helps!
I was also thinking the same thing. Why wouldn’t you just use the .pop[-2] and so forth to get the middle out.
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.
obtain a result.
I am not clear on how slicing works in this exercise especially the end+1 in an array
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.
ok, I think that helps. I went from the solution to the root to figure it out.
Now you have me confused. Please explain. Thanks.
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.
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
15 are removed in the solution. It just happens they are bounded within the list by index
1, inclusive, and index
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.
 + [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
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.
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]
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:]