Advanced Python Code Challenges: Lists, Question 2

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

Seems possible to use del that way:

https://www.programiz.com/python-programming/del

see example 3.

but i think del edits the list in-place. So there is no return value, the existing list gets modified.

Thank you for your reply! :slight_smile:

“but i think del edits the list in-place. So there is no return value, the existing list gets modified.”

Does this mean that its not possible to have it in functions or do you know how the code would look like to work?

Thank you!

Its possible. Just like .sort() sorts the list in memory in-place:

lst.sort()
print(lst) // prints sorted list

del does the same, he existing list is updated.

there is also a good example in the documentation i provided, did you check it out?

Yes, I read it but didn’t quite get how it was supposed to coded in the function (I’m very new to coding). I tried to edit my code a few times but didn’t work.

But after your last comment I tried this code and it worked!

def remove_middle(lst,start,end):
  del lst[start:(end+1)]
  print(lst)
  
#Uncomment the line below when your function is done
remove_middle([4, 8, 15, 16, 23, 42], 1, 3)

Which gives the right ouput! Thank you for your help! :slight_smile:

1 Like

It might print the correct list to the terminal, but it doesn’t actually meet the requirement of the task:

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.

The task asks for a function whichs returns a list object. Your code does not. :slight_smile:

Haha that’s right, thank you for your feedback! :slight_smile: