2. Remove Middle

My question is I can’t understand how this works, please can someone explain logically why the answer gives [4, 23, 42], thank you in advance

Create a function named remove_middle which has three parameters named lst , start , and end .

The function should return a list where all elements in lst with an index between start and end (inclusive) have been removed.

For example, the following code should return [4, 23, 42] because elements at indices 1 , 2 , and 3 have been removed:

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

Hello, @hello1265, and welcome to the forums.

We’ll need a little more to go on in order to offer an explanation. What code was used to produce the output? I could write my own, but that wouldn’t answer your question.

#Write your function here

def remove_middle(lst, start, end):

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

#Uncomment the line below when your function is done

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

You could print each part to help visualize what is happening:

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

#Uncomment the line below when your function is done

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

Output:

[4]
[23, 42]
[4, 23, 42]

The code is concatenating two lists. Each of those lists is a slice of the original list.

2 Likes

i apologise for my stupidity, but I can’t seem to understand still. Why is print(lst[:start]) this printing 4? I would have thought lst corresponds to [4, 8, 15, 16, 23, 42] this part of the parameter and start corresponds to 1 and end to 3. I’ve done 10+ hours of learning today so maybe my brain is fried.

It would be nice to see your attempt at this problem, one that does not use slicing. Think you can come up with a different solution along those lines?

3 Likes

If you are unfamiliar with list slicing, then you should as @mtf suggests, come up with a solution that doesn’t use it. If you were tasked with nailing two boards together, and were provided with a hammer, nails, and a loaded nail gun, which would you use? If you know how to use the nail gun, you’d likely use it. If not, the hammer and nails would work just as well.

2 Likes

Hi, Could you please help me to understand why lst[end+1:] is used?Thank you

I will attempt to explain :slight_smile:
so you have 3 variables: lst, start and end

  • because ‘start’ is removing from the list already you need to add that amount to end, that start is removing from the list.
  • in this case start is removing 1 value from the list, therefore you need to add the value 1 to end, for it to remove the correct amount of values.

I hope this makes sense, and that my understanding checks out.

Okay, thank you.

So if I say, ls[end+2:] supposedly, does it remove end, end+1 but excluding last element?

I’d suggest playing around with list slicing. Read the documentation as well. Try some examples, and see for yourself what happens. Here’s a few:

>>> a = list(range(20))
>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
>>> b = a[:] #copies the entire list
>>> b
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
>>> a[::2] #leaving out start and stop, but adding optional step value
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
>>> a[1::2] #start from the element at index 1 instead of 0
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
>>> #concatenate two slices - the first starts at index 0 and ends at index 3 (exclusive)
... #the second starts at index 10 and goes to the end of the list since no stop value is specified
... a[:3] + a[10:]
[0, 1, 2, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
>>> start = 5
>>> end = 16
>>> a[start:end]
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
>>> a[start:end + 1]
[5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
>>> a[:start] + a[end + 1:]
[0, 1, 2, 3, 4, 17, 18, 19]
>>> a #the original list is unaffected:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
>>>
2 Likes

Thank you. I will definitely look into it.

1 Like