# How to remove specific integers from a list

I’m going through the following:

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 should return `[4, 23, 42]` because elements at indices `1` , `2` , and `3` have been removed:

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

So far I’ve written:

``````#Write your function here
def remove_middle(lst, start, end):
#index = list(range(start, end))
index = lst[start:end]
return index

#Uncomment the line below when your function is done
print(remove_middle([4, 8, 15, 16, 23, 42], 1, 3))
``````

which I feel like is a good start but I do not know the function/syntax that will remove specific values from the index. Is there a function that does this?

Nothing I could find that would be included here:

There’s different ways to remove. You can slice and concatenate the left and right limits. You can also probably pop() the given range in a loop. I think it makes more sense to slice and concatenate and return the result, though.

1 Like

I know slice is [num1:num2] but where does concatenating come into play? Do I just subtract the results that are returned from start:end in a concatenation string?

final_result = lst[start:end] - index

you can return slice1 + slice2

1 Like

Yes but in this case I am being asked to remove the indices that are returned from the slice. I’m not sure I can just concatenate them together. I need to figure out a way to remove them. Maybe I’m thinking about this backwards.

``````slice_left slice_to_remove slice_right

slice_left + slice_right``````
1 Like

@mtf

How do I remove? Is there some type of function that removes from a slice? I know how to start and end a slice but to remove values from the middle hasn’t been explained to me yet.

``````#Write your function here
def remove_middle(lst, start, end):
#index = list(range(start, end))
slice_left = lst[start:]
slice_middle = lst[:]
slice_right = lst[:end]
final = slice_left + slice_right
return final

#Uncomment the line below when your function is done
print(remove_middle([4, 8, 15, 16, 23, 42], 1, 3))
``````

That slice isn’t actually needed. Work with the other two. Make `start` the second index of the first slice and `end` the first index of the second. You may need to offset the first one.

@mtf

I am confused with “make start the second index of the first slice”. What is the second index and what is the first slice in my code?

``````#Write your function here
def remove_middle(lst, start, end):
#index = list(range(start, end))
slice_left = lst[start:]
slice_right = lst[:end]
start = lst[1]
end = lst[0]
final = start - end

#final = slice_left + slice_right
return final

#Uncomment the line below when your function is done
print(remove_middle([4, 8, 15, 16, 23, 42], 1, 3))
``````

We don’t want to mess with the parameters.

``````[:start + 1]

[end:]
``````

Can’t do that. `-` is not a method of list objects.

``````[first_index : second_index]
``````

Any one or both can be absent but the colon is there since that is the slice operator.

[:start + 1]

[end:]

So my guess here is that we are adding 1 to the start value and the end value is now the beginning of the slice?

I’m still unsure what is happening here:

``````#Write your function here
def remove_middle(lst, start, end):
#index = list(range(start, end))
slice_left = lst[:start + 1]
slice_right = lst[end:]
final = slice_left + slice_right

return final

#Uncomment the line below when your function is done
print(remove_middle([4, 8, 15, 16, 23, 42], 1, 3))
``````

This is returning

[4, 8, 16, 23, 42]

so there are still some things left in the middle.

What is the logic with [:start + 1]?

Okay, I messed up the offset. It should be, `end + 1`.

@mtf

``````#Write your function here
def remove_middle(lst, start, end):
#index = list(range(start, end))
slice_left = lst[:start]
slice_right = lst[end + 1:]
final = slice_left + slice_right

# final = slice_left + slice_right
return final

#Uncomment the line below when your function is done
print(remove_middle([4, 8, 15, 16, 23, 42], 1, 3))
``````

This worked but I really wish I could understand what is going on within [end +1:] and [:start]. Do you have an example which you can share or someway to further explain what is going on here?

Now that it’s working, this is one. Standby for mechanics.

1 Like
``````[4, 8, 15, 16, 23, 42]
^   ^   ^
\   \   \
to be removed (1 thru 3, inclusive)
``````

The selected slice subscript would look like,

``````[1:4]    # second index is excluded from slice
``````

The left slice sub would look like,

``````[0:1]
``````

The right slice would look like,

``````[4:6]
``````

That’s in literal terms given the original list. We want our dynamics to compute those values, as applies.

``````[:start]    =>  [0:1]  # look familiar?

[end + 1:]  =>  [4:6]  # ditto
``````

The converse of this would be to actually replace the middle slice with an empty list…

``````lst[1:4] = []
``````

Now, that changes things up…

Only thing, it mutates the list so those middle values will be lost. The concatenation method is an entirely different list and the original is unaltered.

Appendix

``````[start : end]
\       \
include  exclude
``````

Think `range()`.

The `[:]` is a full virtual shallow copy of the list.

Being virtual, the `:` slice operator doesn’t have any exceptions. How could it? It’s virtual. The slice either exists or it doesn’t.

``````>>> a[:256]
[4, 23, 42]
>>> a[256:-256]
[]
>>> lst[1:4] = lst[256:-256]
>>>
``````
3 Likes

Hi @mtf

Thanks so much for this explanation. To be completely clear this concept is incredibly hazy to me. This is the first time I’ve added variables to slices and concatenated them as well. I’m in a much better place than I was at the start of this problem but I’m not 100% there quite yet. Obviously your explanation helped be understand how slices, indexing, and variables work in Python. Thank you very much.

3 Likes

It takes time! You’re putting in good work, just keep at it and it will stick!

1 Like