This community-built FAQ covers the “Remove Middle” exercise from the lesson “Code Challenge: Lists”.
Paths and Courses
This exercise can be found in the following Codecademy content:
Computer Science Data Science
FAQs on the exercise Remove Middle
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply ( ) below!
Agree with a comment or answer? Like ( ) to up-vote the contribution!
Need broader help or resources ? Head here .
Looking for motivation to keep learning? Join our wider discussions .
Learn more about how to use this guide.
Found a bug ? Report it!
Have a question about your account or billing? Reach out to our customer support team !
None of the above? Find out where to ask other questions here !
catower
Split this topic
August 13, 2019, 10:25pm
2
catower
Split this topic
August 13, 2019, 10:26pm
4
4 posts were merged into an existing topic: Why not just use remove()
?
def remove_middle(lst,start,end):
del lst[start: (end+1)]
return lst
This may not be the solution they used but it works.
Pretty sure del was talked about earlier. Either way its self explanatory.
Google is your friend, this is a class not a instant pass to learn Python in 24 hours.
You will need to ask questions.
2 Likes
#Write your function here
def remove_middle(lst, start, end):
if start < len(lst):
one_step = lst[:start]
if end <= len(lst):
two_step = lst[end+1:]
return one_step + two_step
#Uncomment the line below when your function is done
print(remove_middle([4, 8, 15, 16, 23, 42], 1, 3))
1 Like
catower
Split this topic
August 13, 2019, 10:27pm
15
2 posts were merged into an existing topic: Why not just use remove()
?
catower
Split this topic
August 13, 2019, 10:28pm
28
catower
Split this topic
August 13, 2019, 10:39pm
30
#Write your function here
def remove_middle(lst, start, end):
del lst[start:end+1]
return lst
catower
Split this topic
August 13, 2019, 10:37pm
38
catower
Split this topic
August 13, 2019, 10:40pm
47
5 posts were split to a new topic: How does local scope work?
catower
Split this topic
August 13, 2019, 10:41pm
52
My dirty solution? Here it is:
def remove_middle(lst, start, end):
del(lst[start:end+1])
return lst
Yeah, not very smart, but it worked.
This assignment, more so than most, shows how many ways there are to complete a task!
1 Like
Solution using pop()
def remove_middle(lst, start, end):
for i in range(start,end+1):
lst.pop(start)
return lst
catower
Split this topic
August 13, 2019, 10:41pm
58
Am I the only one who didn’t understand what the heck the instructions were saying on this task or the previous one?
6 Likes
Since the instructions are asking to remove a sequential slice, I simply assigned the parameter slice as an empty list to remove it.
def remove_middle(lst, start, end):
lst[start:end+1] = []
return lst
4 Likes
mtf
September 30, 2019, 8:47pm
61
This also makes sense, and doesn’t need +1
in the slice…
>>> def remove_middle(lst, start, end):
lst[start:end] = []
return lst
>>> remove_middle([1, 2, 3, 4, 5, 6, 7, 8, 9], 3, 6)
[1, 2, 3, 7, 8, 9]
>>>
I believe @appylpye once explored this method. As simple as it is, it rarely comes first to mind for most learners.
2 Likes