Remove Middle

Hello all, I am up to LISTS chapter in Python course and came to this challenge which confused me a bit.

Remove Middle
Our next function will remove all elements from a list with an index within a certain range. The function will accept a list, a starting index, and an ending index. All elements with an index between the starting and ending index should be removed from the list. Here are the steps:
1. Define the function to accept three parameters: the list, the starting index, and the ending index
2. Get all elements before the starting index
3. Get all elements after the ending index
4. Combine the two partial lists into the result
5. Return the result

Now I wasnt sure how to do this but the solution they provided is:

def remove_middle(lst, start, end):
  return lst[:start] + lst[end+1:]
print(remove_middle([4, 8, 15, 16, 23, 42], 1, 3))

and explanation:

This can be solved using one line of code if you combine and slice the lists at the same time. Slicing allows us to get the segments of the list before and after the index range and the operation + allows us to combine them together.

I am abit confused what is the longer version of the code and steps. Is someone can enlighten me. :smiling_face_with_three_hearts:

At a guess they mean using temporary variables instead of both slicing and concatenating on the same line, so step 2 would assign the first slice, step 3 would assign the second slice and so on.

This is one version. The pop() method removes the element at the specified position in the list. So, given start and end, we can remove those element(s).

def remove_middle(lst, start, end):
    for i in range(start, end):
        lst.pop(i)

    return lst

A longer version, if this is what you looking for? Initialize an empty list variable to store the new list. Traverse through the original list from beginning to end using range(), when we encounter between start and end, we ignore, otherwise copy (append) the value to the new variable.

def remove_middle(lst, start, end):
    new_lst = []
    for i in range(0, len(lst)):
        if i >= start and i <= end:
            continue
        new_lst.append(lst[i])

    return new_lst
1 Like

Thank you so much for taking the time to reply.
This is excellent, but way to advanced for me, I am not yet on the loops lesson.
My question is more about the Code Academy solution code and how they did it.

def remove_middle(lst, start, end):
  return lst[:start] + lst[end+1:]
print(remove_middle([4, 8, 15, 16, 23, 42], 1, 3))

specifically:

lst[:start]  

^^^^ from my understanding this is from 0 to start, in this case from lst to (but not included) start
and

lst[end+1:]

^^^ most of my confusions lay here
:v:

Try to add the following print() statements into your codes:

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

print(remove_middle([4, 8, 15, 16, 23, 42], 1, 3))

Play around with the last 2 parameters by using different values. You’ll see that it prints out a segment of lst with different start and end using the concept called Slicing.
Eventually, the function returns the entire result list by adding the 2 segments we obtained. By the way, list can be added like:

items = ['cake', 'cookie', 'bread']
total_items = items + ['biscuit', 'tart']
print(total_items)

Result:
['cake', 'cookie', 'bread', 'biscuit', 'tart']
2 Likes