Slicing [N::-1]

J

Joan Miller

What does a slice as [N::-1] ?

It looks that in the first it reverses the slice and then it shows
only N items, right?

Could you add an example to get the same result without use `::` to
see it more clear?

Thanks in advance
 
A

Arnaud Delobelle

Joan Miller said:
What does a slice as [N::-1] ?

It looks that in the first it reverses the slice and then it shows
only N items, right?

Could you add an example to get the same result without use `::` to
see it more clear?

Thanks in advance
l = range(10)
l [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
l[7::-1] [7, 6, 5, 4, 3, 2, 1, 0]
[l for i in range(7, -1, -1)]

[7, 6, 5, 4, 3, 2, 1, 0]
 
S

Steven D'Aprano

l = range(10)
l [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
l[7::-1] [7, 6, 5, 4, 3, 2, 1, 0]
[l for i in range(7, -1, -1)]

[7, 6, 5, 4, 3, 2, 1, 0]


Where does the first -1 come from? Slices are supposed to have default
values of 0 and len(seq):
l[7::1] [7, 8, 9]
[l for i in range(7, len(l), 1)] [7, 8, 9]
[l for i in range(7, len(l), -1)]

[]


I don't believe the actual behaviour is documented anywhere.
 
M

Mensanator

What does a slice as [N::-1] ?

Starts at position N and returns all items to the start of the
list in reverse order.
It looks that in the first it reverses the slice and then it shows
only N items, right?

Wrong. It shows N+1 items. Remember, counting starts from 0.
Could you add an example to get the same result without use `::` to
see it more clear?

for i in range(8,-1,-1):print(a,end=' ')

although I doubt this is more clear.
 
M

Mensanator

l = range(10)
l [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
l[7::-1] [7, 6, 5, 4, 3, 2, 1, 0]
[l for i in range(7, -1, -1)]

[7, 6, 5, 4, 3, 2, 1, 0]


Where does the first -1 come from? Slices are supposed to have default
values of 0 and len(seq):


The only way to get a 0 from a reverse range() is to have a bound of
-1.
l[7::1] [7, 8, 9]
[l for i in range(7, len(l), 1)] [7, 8, 9]
[l for i in range(7, len(l), -1)]


[]

I don't believe the actual behaviour is documented anywhere.


Well, it's implied. If the stopping bound in a reverse range()
is greater than the starting bound, you get an empty return.
 
R

Robert Kern

l = range(10)
l [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
l[7::-1] [7, 6, 5, 4, 3, 2, 1, 0]
[l for i in range(7, -1, -1)]

[7, 6, 5, 4, 3, 2, 1, 0]


Where does the first -1 come from? Slices are supposed to have default
values of 0 and len(seq):


Rather, they have 0 and len(seq), respectively, when the step is positive, and
len(seq)-1 and -1 when the step is negative.
l[7::1] [7, 8, 9]
[l for i in range(7, len(l), 1)] [7, 8, 9]
[l for i in range(7, len(l), -1)]

[]


I don't believe the actual behaviour is documented anywhere.


True, I don't think it is.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
G

Gary Herron

Mensanator said:
l = range(10)
l

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

l[7::-1]

[7, 6, 5, 4, 3, 2, 1, 0]

[l for i in range(7, -1, -1)]

[7, 6, 5, 4, 3, 2, 1, 0]

Where does the first -1 come from? Slices are supposed to have default
values of 0 and len(seq):


The only way to get a 0 from a reverse range() is to have a bound of
-1.


Not quite. An empty second bound goes all the way to the zero index:
[2, 1, 0]

Gary Herron



[7, 8, 9]
[l for i in range(7, len(l), 1)]

[7, 8, 9]
[l for i in range(7, len(l), -1)]

[]

I don't believe the actual behaviour is documented anywhere.


Well, it's implied. If the stopping bound in a reverse range()
is greater than the starting bound, you get an empty return.

 
R

Robert Kern

l = range(10)
l
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
l[7::-1]
[7, 6, 5, 4, 3, 2, 1, 0]
[l for i in range(7, -1, -1)]
[7, 6, 5, 4, 3, 2, 1, 0]


Where does the first -1 come from? Slices are supposed to have default
values of 0 and len(seq):


Rather, they have 0 and len(seq), respectively, when the step is
positive, and len(seq)-1 and -1 when the step is negative.


Well, not entirely true. [N:-1:-1] obviously doesn't work for this. Rather,
leaving the second argument in the slice empty means "go to the end if step > 0
or go to the beginning if step < 0". There is no explicit translation of the
latter because there is no numerical index for the element before the first element.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
M

Mensanator

Not quite.  An empty second bound goes all the way to the zero index:

Not the same thing. You're using the bounds of the slice index.
I was refering to the bounds of the range() function.
9 8 7 6 5 4 3 2 1 0 -1 -2 -3 -4 -5 -6 -7 -8

To get that to stop at 0, you use a -1 as the bounds:
9 8 7 6 5 4 3 2 1 0

Your slice notation only works if the last (first?) number in
the range happens to be 0. What if the range bounds were variables?
You may still want to force the range's last number to be 0 by
using a constant like range(a,-1,-1) rather than just take
the last number of range(a,b,-1) by using slice notation.
 >>> range(9)[2::-1]
[2, 1, 0]

Gary Herron
 
G

Gary Herron

Mensanator said:
Not the same thing. You're using the bounds of the slice index.
I was refering to the bounds of the range() function.


9 8 7 6 5 4 3 2 1 0 -1 -2 -3 -4 -5 -6 -7 -8

To get that to stop at 0, you use a -1 as the bounds:


9 8 7 6 5 4 3 2 1 0

Your slice notation only works if the last (first?) number in
the range happens to be 0. What if the range bounds were variables?
You may still want to force the range's last number to be 0 by
using a constant like range(a,-1,-1) rather than just take
the last number of range(a,b,-1) by using slice notation.

All true and valid of course, but I was just contridicting the "the
ONLY way to get a 0" (emphasis mine) part of the statement.

Gary Herron


range(9)[2::-1]
[2, 1, 0]

Gary Herron
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,176
Messages
2,570,949
Members
47,500
Latest member
ArianneJsb

Latest Threads

Top