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[i] for i in range(7, -1, -1)]
[7, 6, 5, 4, 3, 2, 1, 0]
--
Arnaud
> What does a slice as [N::-1] ?
Why don't you try it?
>>> s = "abcdefgh"
>>> s[4::-1]
'edcba'
The rules for extended slicing are not explained very well in the docs,
and can be confusing. In my experience, apart from [::-1] it is best to
always use a positive stride (the third number).
--
Steven
>>>> 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[i] 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[i] for i in range(7, len(l), 1)]
[7, 8, 9]
>>> [l[i] for i in range(7, len(l), -1)]
[]
I don't believe the actual behaviour is documented anywhere.
--
Steven
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[i],end=' ')
although I doubt this is more clear.
>
> Thanks in advance
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[i] for i in range(7, len(l), 1)]
> [7, 8, 9]
> >>> [l[i] 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.
>
> --
> Steven
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[i] for i in range(7, len(l), 1)]
> [7, 8, 9]
>>>> [l[i] 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
Not quite. An empty second bound goes all the way to the zero index:
>>> range(9)[2::-1]
[2, 1, 0]
Gary Herron
> Rather, they have 0 and len(seq), respectively, when the step is
> positive, and len(seq)-1 and -1 when the step is negative.
>> I don't believe the actual behaviour is documented anywhere.
>
> True, I don't think it is.
There are at least two open issues.
http://bugs.python.org/issue1446619
http://bugs.python.org/issue7460
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.
Not the same thing. You're using the bounds of the slice index.
I was refering to the bounds of the range() function.
>>> for a in range(9,-9,-1):print(a,end=' ')
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:
>>> for a in range(9,-1,-1):print(a,end=' ')
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
Does it still contradict if you do not use the '::' as the OP
requested?
>
> Gary Herron
>
>
>
>
>
> >> >>> range(9)[2::-1]
> >> [2, 1, 0]
>
> >> Gary Herron- Hide quoted text -
>
> - Show quoted text -- Hide quoted text -
>
> - Show quoted text -
Not to my knowledge... I believe your statement is true in all cases
which explicitly state the stop value.