Advance Python List Challenges: Exercise 2 - Remove Middle

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

The exercise says to include the final index in the removed segment.

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

so our final list should exclude those three values.

[4, 23, 42]

One way to look at this is to loop through all the values but only keep the ones that are not at those indices. Start with an empty list.

a = []

Now we can iterate the list and if the index is not 1, 2, or 3, we’re away. Bear in mind that this is a function so the indices may vary. We need to keep it dynamic, not literal.

>>> def foo(u, s, e):
    a = []
    for i in range(len(u)):            #  all the indices
        if i not in range(s, e + 1):   #  the excluded indices
            a.append(u[i])             #  the included indices
    return a

>>> foo([4, 8 , 15, 16, 23, 42], 1, 3)
[4, 23, 42]

Just interesting what is the difference between inclusive and exclusive?

They’re what the names imply. Inclusive means you include the numbers specified and exclusive means you don’t. For example:

  • 1-5 inclusive includes the numbers 1, 2, 3, 4, and 5.
  • 1-5 exclusive includes only the numbers 2, 3, and 4.

That I meant. Thank you, Victoria! I also researched some articles and found this review
about my question.

1 Like