Why and how are selecting python list indexes inclusive and exclusive?

Question

In python, how are lists inclusive/exclusive when referencing the index? and why?
Or why do I need to use +1 to the index at the end of a selection in my list?

Answer

When selecting from a list in python, you may have noticed the saying ‘inclusive’ or ‘exclusive’, this is usually referring to the slice notation mylist[start:stop] where the start index is inclusive but the stop index is exclusive. What does this mean?

If an index is inclusive, then that number will be included in the selection, while an exclusive will not be. Say, for example, we have the following list and two list slices.

list = [0, 1, 2, 3, 4, 5]

list1 = list[:3] # [0, 1, 2]
list2 = list[4:] # [4, 5]

As you can see, the stop index of 3 is exclusive so the third index item (conveniently equal to 3) is not selected in list1. On the other hand the 4 is included in list2 since that number is inclusive.

Now lets say you want to select something based on the index number, since you know what that is. We want our new list to include start and stop so lets see:

start = 1
stop = 3
list3 = list[start:stop] # [1,2]

Unfortunately, this doesn’t include our desired stop since it is exclusive, so how can we include it? by shifting the index up 1.

list3 = list[start : stop + 1] #[1, 2, 3]

Finally, you may be asking why the developers would have made it this complicated? The best explanation comes from looking at half-open intervals:

list[:3] #the first 3 characters
list[3:] #everything after the first 3 characters

I hope this all makes sense, please continue the discussion below and help each other out!

6 Likes

This section of challenges has been incredibly frustrating to me. I went through the making and modifying lists instructions twice because I felt like I missed an entire segment of this lesson.
The language used to guide me through the code is very confusingly put. Maybe my brain is having trouble translating these problems into something that makes sense.
But for the life of me, I do not understand why the solution uses:
lst[end+1:]
I don’t understand why we are adding 1 to the end slice of lst. This function is made to output a lst with a specific set of indices and doesn’t seem to really work if using a different set of numbers.
Some clarity would be greatly appreciated.

23 Likes
def remove_middle (lst, start, end):

The name suggests that all elements, including the one at index position, end will be removed. Recall that list slices do not include the index at end, but stop just before that position. If we do not add 1 then the element at position end will not be in the list slice.

4 Likes

I felt kind of the same way. But in the end I came up with this:

def remove_middle(lst, start, end):
    area_1 = 0 + start
    area_2 = ((len(lst)) - end - 1) * (-1)
    new_lst = lst[:area_1]
    new_lst2 = lst[area_2:]
    return new_lst + new_lst2

Seems a bit complicated but it work. Any suggestion how to simplify it?

2 Likes

Best suggestion is to think it through, piece by piece and determine what, if anything could be done differently. Us telling you how to simplify won’t help you. That’s where the work begins in learning to program.

Start with something that works, then tear it down and build it back up again, only different, each time. Eventually you will have a storehouse of alternate approaches, some simpler than others. Weigh out the pro’s and con’s for each, and you will be officially learning.

6 Likes

thanks for the quick reply;) I’ll try my best.

2 Likes

Why so complicated??

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

7 Likes

Wow i just spent so long trying to figure this one out. It’s the first time i have been completely stumped during this course and came to check the answer.

Cant believe you solved it with just a 1-liner - great :slight_smile:

2 Likes

It’s not just you, this section of challenges was very poorly organized. Every single one of them requires knowledge that was not taught in the lessons. Maybe they’re trying to teach us to be resourceful?

4 Likes

So if we can use the “+” to add things from a list into another list why cant we use the minus symbol ( - ), in order to remove an area of a list we dont want?

1 Like

Bro how much did this take for you to write that code? the area_1 is start but the area_2 gives negative number always. i dont know how the fk this code works but it does lol nice

The plus sign is a concatenation operator. Concatenation is the process of linking two objects: a string to a string, or a list to a list. There is no operation to minus an element from a list, save manual iteration or using a method such del(), .pop(), or .remove().

3 Likes

I tried to use delete, because in a previous lesson we were taught the following:
" Emptying a list

There are two ways to empty an existing list. The first way is to just assign the list variable to an empty list. In this case, the old list and its contents are automatically deleted by Python. This is shown in the following code example. list1 = [1, 2, 3, 4, 5, 6, 7 ]
list1 = [ ]
The second method is to use the del() function to delete all the elements of the list. This forces the removal of all the items immediately instead of letting Python delete the contents at some time of its choosing. Both methods produce the same end result which is that the list will be empty of contents.
list2 = [ 10, 20, 30, 40, 50, 60, 70 ]
del list2[:]"

but obviously that command can’t be right with the space between del and list because python hates spaces. can this even work if put in correct syntax?
lst = del(lst[start:end+1]) ?

1 Like
>>> s
'abcdefghijklmnopqrstuvwxyz'
>>> t = list(s)
>>> del(t[7:17])
>>> t
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
>>> ''.join(t)
'abcdefgrstuvwxyz'
>>> 
>>> t = list(s)
>>> del (t[7:17])     # don't leave out the parens; space is not a problem
>>> ''.join(t)
'abcdefgrstuvwxyz'
>>> 
3 Likes

thank you!

I don’t understand this part, though: ‘’.join(t)
I don’t think that was in a lesson yet. what is it supposed to do?

The str.join() method takes a list of strings and concatenates them into a single string. The string object it acts upon is the separator, in this case an empty string.

It can also be used to insert characters into a string…

>>> '-'.join('catnip')
'c-a-t-n-i-p'
>>> 
2 Likes

thanks :slight_smile:

2 Likes

This took me a while to figure out. I finally get it now. The +1 at the end is to adjust for the final number “end” to be removed from the function. To some of the other threads, I was tempted to use additional functionality, but I assume there is a method to his madness. This is so far the hardest exercises, primarily because of the wording.

2 Likes

What you need to understand here is that [:start] gets everything UNTIL start (not including start) , and [end:] gets everything FROM end until the end (including end). But since we want to erase the middle, we don’t want to include end.

Here is what I’ve got:

def remove_middle(lst, start, end):
  first = lst[:start]
  second = lst[(end+1):]
  return first + second
3 Likes

How would I know to use ‘+1’ after [end] for this problem? What does adding a ‘+number’ when I’m slicing lists? Hope this makes some sense.

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

Thanks for the help.