Hello, I have a question regarding Question 2 in Advanced Python Code Challenges: Lists. I have tried to search for an answer in other threads but couldn’t find anything.
Question 2, Remove Middle is as follows:
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 remove_middle([4, 8 , 15, 16, 23, 42], 1, 3)
should return [4, 23, 42]
because elements at indices 1
, 2
, and 3
have been removed.
This is the right answer:
#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))
But I tried to do it by using del
:
#Write your function here
def remove_middle(lst,start,end):
new_list = del lst[start:(end+1)]
return new_list
#Uncomment the line below when your function is done
print(remove_middle([4, 8, 15, 16, 23, 42], 1, 3))
However this didn’t work, I got this Syntax error message in return:
File "script.py", line 3
new_list = del lst[start:(end+1)]
^
SyntaxError: invalid syntax
Is it something I’m missing when using del
or can it not be used the way I used it?
Thank you in advance!
// Despina