FAQ: Code Challenge: Lists - Remove Middle

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 (reply) below!

Agree with a comment or answer? Like (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!

16 posts were merged into an existing topic: Why and how are selecting python list indexes inclusive and exclusive?

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

2 posts were merged into an existing topic: Why not just use remove()?

A post was merged into an existing topic: Why and how are selecting python list indexes inclusive and exclusive?

2 posts were split to a new topic: What does 1, 3 mean in the lists code challenge?

#Write your function here

def remove_middle(lst, start, end):
del lst[start:end+1]
return lst

4 posts were merged into an existing topic: Why and how are selecting python list indexes inclusive and exclusive?

5 posts were split to a new topic: How does local scope work?

A post was merged into an existing topic: Why and how are selecting python list indexes inclusive and exclusive?

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

4 posts were split to a new topic: Can I use - on lists (subtract) [code challenge lists]

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

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

Yes, the post is here.

1 Like