From: Mensanator on
On Mar 5, 12:28 pm, Steven D'Aprano <st...(a)REMOVE-THIS-
cybersource.com.au> wrote:
> On Fri, 05 Mar 2010 18:12:05 +0000, Arnaud Delobelle wrote:
> >>>> 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):

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

From: Robert Kern on
On 2010-03-05 12:28 PM, Steven D'Aprano wrote:
> On Fri, 05 Mar 2010 18:12:05 +0000, Arnaud Delobelle wrote:
>
>>>>> 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):

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

From: Gary Herron on
Mensanator wrote:
> On Mar 5, 12:28 pm, Steven D'Aprano <st...(a)REMOVE-THIS-
> cybersource.com.au> wrote:
>
>> On Fri, 05 Mar 2010 18:12:05 +0000, Arnaud Delobelle wrote:
>>
>>>>>> 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):
>>
>
> 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:

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

Gary Herron




>
>>>>> 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
>>
>
>

From: Terry Reedy on
On 3/5/2010 2:10 PM, Robert Kern wrote:

> 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



From: Robert Kern on
On 2010-03-05 13:10 PM, Robert Kern wrote:
> On 2010-03-05 12:28 PM, Steven D'Aprano wrote:
>> On Fri, 05 Mar 2010 18:12:05 +0000, Arnaud Delobelle wrote:
>>
>>>>>> 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):
>
> 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