0

I have a list object which looks as below:

lst = [50,34,98,8,10]

The output for the statement print(lst[:5:-2]) is coming as blank []

My understanding is that print(lst[:5:-2]) translates to print(lst[-1:5:-2])

This means:

  • start from index -1 # this corresponds to value 10
  • stop at index 4 # this corresponds to value 10
  • perform increment of -2

As start and stop are pointing to 10 so I am expecting the output to be 10 here.

3
  • Does this answer your question? tutorial.eyehunts.com/python/… Commented Sep 22, 2021 at 4:06
  • The syntax is lst[start:stop:step] (I didn't really understand what increment was) Commented Sep 22, 2021 at 4:07
  • 2
    Start at index -1 (i.e. 4). Stop before reaching index 5. There are no elements between 4 and 5 going backwards (just like there are no indices between 1 and 0, going forwards, which is why lst[1:0] returns empty). The first range that would include an element is lst[:3:-2] (start at 4, stop before 3, 4 is before 3 going backwards, thus lst[4] gets included). Commented Sep 22, 2021 at 4:07

2 Answers 2

2

An easy way to understand python slicing syntax is to think about it as such: slice[start:stop:step].

What this means is that your starting point is start, here that's index -1 since the starting position for a negative step is -1. It stops at stop, here that's the 4th element. When using a step of -2 the first iteration will be the 4th element. Thus nothing is returned since you've already arrived at your stop condition.

Sign up to request clarification or add additional context in comments.

Comments

0

Your understanding is not wrong, the request print(lst[:5:-2]) is equal to print(lst[-1:5:-2]). The question here is, why the result is empty not [10].

Because python also can use negative indexing, and last item is indexed as -1... till the first item indexed as -len(list).

Your list is lst = [50,34,98,8,10] having length of 5 (5 items) so then the last item index is 4. That's why, when you use slice print(lst[-1:5:-2])

Start index: start at -1, the -1 is equal to 4. End index: is 5 Step: -2

because the step is -2 so the movement should be backward, but then the start index already <= end index(start <= stop), so no item is returned because it won't move any where.

For example to get [10] you should do it like print(lst[:3:-1])

you can read more documentation here: python 3 slice, start stop step from eyehunt toutorial, geeksforgeeks python slicing

4 Comments

My understanding is that when we have a negative step then the start position is -1 and not 0. Please correct
If that's the case, why does lst[:5:-1] return [] but lst[:3:-1] return [10] ?
@Underoos because in python, there exists negative index too. So I will update the answer. Corresponds to the right meaning of negative step
I fixed my answer, I give you more reason why that your command do not get any item in result list

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.