I can't under stand this function

Could you please explain it?

I can’t under this function

|1|def remove_middle(lst, start, end):expressionless:

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

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

lst ==> should be all of this right ? ([4, 8, 15, 16, 23, 42], 1, 3))

why does it retrieve only [4,8,15,16,23,42] ??

and how the “start” defines as “8”

and "end’ defines as “16”

Could you please explain for me ??

Consider that

start = index 1
end = index 3

and remember arrays are 0 indexed.

Let me know if you need further explanation.

how can you define start is index1 ?
end is index 3??

since i didn’t define in the function

that is my question

i wonder how this result come out ?

without any define it ?

when you invoke remove_middle with the statement remove_middle([4, 8, 15, 16, 23, 42], 1, 3)

your lst is [4, 8, 15, 16, 23, 42]
your start is 1
and your end is 3

remember the declaration of the function is remove_middle(lst, start, end)

2 Likes

Here’s some code that describes the slicing process if that’s what you’re having trouble with, just paste it into a sandbox for output. Python Sandbox | Codecademy

def remove_middle(lst, start, end):

  #index and list items for the whole list
  for num in range(len(lst)):
    print(num, lst[num]) 
  print("\n")       #prints empty line

  #index and list items for the first slice lst[0 : start] (0 is default, added it for clarity)
  for num in range(0, start):
    print(num, lst[num])
  print("\n")

  #index and list items for the second slice list[end+1 : len(lst)] (end of list is default, added len(lst) for clarity)
  for num in range(end+1, len(lst)):
    print(num, lst[num])
  print("\n")
  
  #the return adds the 2 slices together
  return lst[ : start]+lst[end+1 : ]

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