From what i can tell, this code gives no indication that it should see the middle number of a list- how does it know to do so?

#Write your function here

def remove_middle(my_list, start, end):

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

#Uncomment the line below when your function is done

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

output was:
[4, 23, 42]

It knows this because you defined a function that takes in three arguments. In this case it’s a list as my_list, an int as start, and an int as end. The function is returning the data by “list slicing” to parse the data.

It looks correct to me. Perhaps reviewing the “Working with Lists in Python” Lesson in the “Learn Python 3” syllabus might help you.

Additional info: list[:n] will count to n’s index and include everything before it. list[n:] will slice value n’s index and include everything to the right of it inclusive.

I don’t know if I explained it well, but I included it in case you didn’t want to review “Working with Lists in Python”.

1 Like

ah, so it’s not so much targetting the middle as much as just targetting the index that its told to which happens to be the middle?

Close. When slicing a list with the “list[n:], list[:n], list[n:n]” (you can use a number after another ":"operator to denote syntax it’s targeting a specified index and then depending on which side of the “:” operator the number is on determines which direction the list will be sliced.

For example because my_list[end+1:] would be starting at index 4 include everything to the right.
Also, my_list[:start] would be starting at index 1 and includes everything to the left of it. (Note that this will not include index one, but instead everything to the left of it.)

Additionally, you could grab the remaining numbers in the list by slicing it like this my_list[start:end+1]. The slice will start at index one and include everything to right right of it, but because there is an index (which is derived by end+1) on both sides of the “:” operator it stops at the second index. Not that the slice is exclusive with respects to the index on the right side of the “:” operator.

1 Like

I said “Not that the slice is exclusive with respects to the index on the right side of the “:” operator.”, But I meant “Note that the slice…”