CS101 Lists Coding Challenge

So, first time I ask for help here.

The function is supposed to remove from the list the elements between index positions ‘start’ and ‘end’.

I though about setting up a loop and let it go through the indices and removing an element each time, but the loop does not do what I want it to do and it’s frustrating me a lot.

What did I miss? I know it’s probably something super-banal, but I can’t figure it out.

Thank you for the help

https://www.codecademy.com/paths/computer-science/tracks/cspath-cs-101/modules/cspath-code-challenges/articles/advanced-python-code-challenges-lists

I just used a return statement.
Perhaps I was lazy. :rofl:

def remove_middle(lst, start, end):
return lst[:start] + lst[end+1:]

Summary

Further: [:start] will give you the item at the beginning of the list to the stop point and [end+1:] will give you the end of the list (whatever the number is) plus 1 (ie: moving to the right).

  1. Define the function to accept three parameters: the list, the starting index, and the ending index
  2. Get all elements before the starting index
  3. Get all elements after the ending index
  4. Combine the two partial lists into the result
  5. Return the result

Notice the start and end arguments for your function are indices?

Lets walk through your function and try to figure out what it’s doing.

def remove_middle(lst, start, end):
    i = start
    while i <= end:
        lst.pop(i)
        i += 1
    return lst

print(remove_middle([4, 8, 15, 16, 23, 42], 1, 3))

Our function takes 3 positional arguments lst, start and end. With the current print statement using to test this:
lst= [4, 8, 15, 16, 23, 42]
start = 1
end = 3

Bearing that in mind, lets follow the logic of your code and see what it’s doing

The first time the loop runs you start at index 1 (remember indexing starts at 0!)
We take our the list passed to our function, remove the element at index 1 and increase the index by 1.

Now when the loop runs again it is not using the same copy of our list! Remember you both removed an element at index i and increased the index by 1 !

So when the loop runs again:
start = 2
end = 3
lst = [4, 15, 16, 23, 42]

So if we do the same thing again what do you think will happen?

As @lisalisaj already gave you, the solution is slicing and combing the lists.

2 Likes

I mean, it’s the same way I solved it, and the same way CA did as well :smiley:

def remove_middle(lst, start, end):
    lst1 = lst[:start]
    lst2 = lst[end+1:]
    lst3 = lst1 + lst2
    return lst3


print(remove_middle([4, 8, 15, 16, 23, 42], 1, 3))

In addition to the solution provided by @lisalisaj the above also works. The two functions do the same thing, the solution provided by lisa is just simpler to write.

I also didn’t feel like writing a loop b/c I felt that wasn’t the best way. (imo). The way I wrote it made the most sense to me. :woman_shrugging:t2:

1 Like

You don’t even need a loop :slight_smile: see my previous comment

Yes, in the end I did it like this, but I wanted to do it with a loop, until I gave up and used the slicing method.

Perfect, now I get it. My mistake was to think that the indices in the list would refer to the same elements, but they change because the list itself changes. Thank you!

2 Likes

Yes! You’re correct, the reason your loop failed is because you were changing the list being used, which caused your indices to be wrong. While you could create a copy of the list that’s being used, then modify and return that instead (so the list being used by your function doesn’t change) to also fix the issue… but K.I.S.S. :slight_smile:

2 Likes