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

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:]

8 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 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().

2 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'
>>>
2 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'
>>>
1 Like

thanks 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.

3 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
2 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.

If we use end as given, then the new combined string will contain the letter at index end, which we do not want. We need to exclude all the characters between start and end, including the characters at those indices, hence, end + 1.

Sketch it out on paper to give yourself a better picture.

['abcdefg'][start ('h') 'ijklmnopqr' end('s')]['tuvwxyz']
.. 6            7                    16    17 ..

Recall that a slice excludes the stop index so when we write, str[:7] it gives abcdefg, and when we write, str[17:] it gives tuvwxyz.

The removed substring should be 10 characters long,

len('hijklmnpqr')    # 10
2 Likes

The above post is so full of errors it’s embarrassing.

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

>>> s = 'abcdefghijklmnopqrstuvwxyz'
>>> remove_middle(s, 7, 17)
('abcdefgrstuvwxyz', 'hijklmnopq')
>>> a, b = remove_middle(s, 7, 17)
>>> len(a); len(b)
16
10
>>>

One clearly sees why this question came up. Intuition, or something of that nature. How I plunged headlong in defense with blinders on is my bad. Didn’t feel right afterward so came back for another look see. Lucky I did.

Visualization

>>> def remove_middle(x, h, k):
return x[:h], x[h:k], x[k:]

>>> l, m, r = remove_middle(s, 7, 17)
>>> '{} {} {}'.format(l, m, r)
'abcdefg hijklmnopq rstuvwxyz'
>>>

Now we need to get this to work on a list.

>>> l, m, r = remove_middle(list(s), 7, 17)
>>> l + r
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
>>> m
['h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q']
>>>
1 Like

Remember that you can select a slice of a list with lst[index1:index2] You can additionally do maths in this selection such as lst[index1:index2 +1]

Basically you are saying we have to do the +1 because the end index is exclusive.

I tackled this problem differently and came up with this:

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

I love what @code_colt has done, converting the end index to a negative value. It took me some time to wrap my head around what you did. Once I got it, I had the exact opposite question: is my code to simple? Are there cases where @code_colt’s code would prove more functional than mine?