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.
lst[start:stop:step](I didn't really understand what increment was)-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 whylst[1:0]returns empty). The first range that would include an element islst[:3:-2](start at 4, stop before 3, 4 is before 3 going backwards, thuslst[4]gets included).