0

If negative list indexing is starting from the end the list and let's say we have:

l = [1,2,3,4,5]

and l[0:3] and l[:3] returns same values while l[-3:0] returns an empty list and l[-3:] returns [3,4,5]

What is the logic behind not allowing list[-x:0] to return the list?

Rarblack
  • 4,559
  • 4
  • 22
  • 33

4 Answers4

2

l[-3:0] tries to slice from 3 from behind towards 0 - that is the same as l[2:0] .. that slices nothing because the first value > second value.

l[-3:] can be read as l[-3:len(l)] - so l[2:5] which returns the slice.

You would need l[-3:0:-1] for that to work - but thats mind boggling slicing which I try to avoid. ( print( [1,2,3,4,5][-3:0:-1] --> [3, 2] ) because it also reverses the slice "orientation" to backwards instead of forwards

l[-3:] slices from 3 from behind till the end.

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
1

The full notation of slice in Python is the following:

s[start:end:step]

That being said it provides useful defaults for the values, as per the documentation:

Slice indices have useful defaults; an omitted first index defaults to zero, an omitted second index defaults to the size of the string being sliced.

So when you do something like:

s[1:]

under the hood this is done:

s[1:len(s)]

Note that in both cases step defaults to 1. In most languages when you want to access the last element of a list for example you do something like:

s[len(s) - 1]

Python negative indexing is a sort of syntactic sugar on that notation so :

l[-1] = l[len(l) - 1]
l[-2] = l[len(l) - 2]
...

Then when you do:

l[-3:]

this is done:

l[len(l)-3:len(l)]

So, instead of 0 you should use len(l) as the last index:

l = [1, 2, 3, 4, 5]
print(l[-3:len(l)])

Output

[3, 4, 5]

Note that l[-3:0] returns the empty list because len(l) - 3 > 0, i.e. the first index is greater than the second and step is 1.

Further

  1. Understanding Python's slice notation
Dani Mesejo
  • 61,499
  • 6
  • 49
  • 76
0

Well 0 in front works, but at last... won't work, same with this:

>>> l=[1,2,3,4,5]
>>> l[2:0]
[]
>>> 

because python is thinking zero at the end...

So that's virtually equivalent to:

>>> l=[1,2,3,4,5]
>>> l[:0]
[]
>>> 

since of course there is nothing before first element, if there was, that thing wouldn't be first element, it would be second.

U13-Forward
  • 69,221
  • 14
  • 89
  • 114
0

Negative indexes operate from the end of the list. Say if you want to get the last 3 items, you could do:

# Reversing and restoring the order
my_list = [1,2,3,4,5,6]
print(reversed(reversed(my_list)[:3]))

# Subtracting from the size
size = len(my_list)
print(my_list[size-3:])

But instead you can type

my_list[-3:]

Which in words would state get me the slice of the list starting from the third item at the end of the list

So you have to be aware of what your asking for.

# Slice from the x'th element at the end till the end of the list
my_list[-x:]

# Slice from the x'th element at the end till the y'th element at the end of the list
my_list[-x:-y]

# Slice from the x'th element at the end till the y'th element from the start of the list. 
# Only valid if len(my_list) - y < x and y > 0
my_list[-x:y] 
ebro42
  • 81
  • 1
  • 3