Slicing: Logic of omitting 0

I have just completed this lesson on slicing lists, in which it is demonstrated that omitting 0 from the beginning of a slice is perfectly acceptable.

As in:

my_list = ['ball', 'hammer', 'wood', 'pin']

#List containing ['ball', 'hammer'] 
my_list[:2]

But I am slightly confused about selecting elements from the end or lists - particularly, I am wondering if not including an end index in a slice is the same as having the last index be 0…?

For instance, I am curious about the differences between the code blocks below:

suitcase = ['shirt', 'shirt', 'pants', 'pants', 'pajamas', 'books']

#Prints ['pajamas', 'books']
end = suitcase[-2:]
print(end)

But when I include zero as the final index, I get an empty list:

suitcase = ['shirt', 'shirt', 'pants', 'pants', 'pajamas', 'books']

#Prints []
end = suitcase[-2:0]
print(end)

Why might this be?

Because the order is not changed by a third, negative argument for the direction (one possible way).

If we want the last two, leave that argument empty.

>>> suitcase = ['shirt', 'shirt', 'pants', 'pants', 'pajamas', 'books']
>>> suitcase[-2:]
['pajamas', 'books']
>>>

The last two, from left to right.

>>> suitcase[-2:0:-1]
['pajamas', 'pants', 'pants', 'shirt']
>>> 

Notice this goes in reverse (right to left) but leaves off the last and the first elements.

3 Likes

You mention that ‘the order is not changed by a third argument’; however, I am having trouble understanding how including the 0 is the same as having a third argument. I think I may be misunderstanding what you mean.

I am also not entirely clear on the second example with the reverse list.
Does [-2:0:-1] translate to ‘two from the end to the 0 index, slip the last element’ ?

‘Skip’ the first element is what is says. The ‘end’ index is never included. If we want to include index 0, then we tell Python that the end index is -1.

Let me rephrase. Yes, skip the last element as indicated by the parsing direction, right to left. In any case, the first element would not be in the slice.


Examine the overall list. We know that we can access it in both directions, right-to-left, and left-to-right.

>>> a = [1,2,3,4,5,6,7,8,9]
>>> b = a[1:]
>>> b
[2, 3, 4, 5, 6, 7, 8, 9]
>>> c = b[-2::-1]
>>> c
[8, 7, 6, 5, 4, 3, 2]
>>> d = c[-2:0:-1]
>>> d
[3, 4, 5, 6, 7]
>>> 

Recall that slices are vectors. We must always keep direction in mind.

2 Likes

not changed by a third argument, as in there is none, but should be, which is by now rather moot.

Okay, I think I am beginning to follow.
So, my issue with the first suitcase list was that I needed a third negative argument?

Where I had:

>>> suitcase = ['shirt', 'shirt', 'pants', 'pants', 'pyjamas', 'books']
>>> suitcase[-2:0]
[]

I should have had:

>>> suitcase[-2:0:-1]
['pyjamas', 'pants', 'pants', 'shirt']

?
Here we are starting with the second to last index from the right (so the very last would not be the ‘0’ index if starting from the right?), through to the ‘0’ index (exclusive), and the -1 is what starts the list ‘backward’ (right to left)?

If this is the case, why then would suitcase[-2:0] not give the second to last element through to the end of the list in the left to right direction? That is ['pyjamas', 'books'] ?

Whatever you do, don’t leave this study until all the bases are covered.

Negative/positive indices

 -5, -4, -3, -2, -1
[ a,  b,  c,  d,  e ]
  0,  1,  2,  3,  4
2 Likes

Ah, thank you for laying it out like this.

When using negative indices, is there no such thing as the 0 index? Is that why I was getting an empty list?

Yes, the 0 index only belongs to LTR. Just as -5 only belongs to RTL, above.

Okay! The water is started to become clearer.
So, returning to my very first suitcase list, the issue was that it did not have a argument to specify the direction and it also included a 0?

1 Like

Yes, because the zero was in the end argument, after the -2 in the start argument, there was no slice that could be had given that criteria. It won’t raise an error since slices are virtual. They act upon the directives we supply and ignore any that while valid integers, have no real meaning in the slice. By adding the direction we make it clear what our intention is.

1 Like