Just been working my way through Advanced Python Code Challenges: Lists. I’m slightly confused with the answer to the task listed below:
Create a function named
remove_middle which has three parameters named
start , and
The function should return a sub-list of
lst with all elements with index between
end removed (inclusive).
For example, the following code should return
[4, 23, 42] because elements at indices
2 , and
3 have been removed:
removeMiddle([4, 8 , 15, 16, 23, 42], 1, 3)
The correct code is:
def remove_middle(lst, start, end): return lst[:start] + lst[end+1:] print(remove_middle([4, 8, 15, 16, 23, 42], 1, 3))
My question is how does this remove the elements with an index between ‘start’ and ‘end’?
I thought this would return the elements between ‘start’ and ‘end’ not remove them?
I assume I’m missing something obvious, Lists are something I’m struggling to grasp!
Any help would be greatly appreciated.