List index beginner

Hello!Please,help to understand,what does this part of code do lst[end+1:],all the other is Ok)Thank you!Why do we have output 4,23,42 not 4,42,1?

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))

I suggest you review slice syntax

there is no a 1 in the input list, how could it appear in the list on its own?

It is important to remember that lists are zero indexed, meaning that the first element is at position 0 and the second at position 1 etc.

Slicing is not inclusive, which means it will start with the first index and go up until, but not include, the last index.

lst[:3]

The above would start at the beginning (lists being zero indexed) and go up to the element at position 3 (which is 16 in your example) but not include it:

Result:

[4, 8, 15]

You are doing this twice and then concatenating (joining) this lists.

Taking the above into consideration, all the +1 does in your code is make the function inclusive, so that it goes up to and includes the element at the index you have provided.

1 Like