[ python-Bugs-1303928 ] Extended slice bug (or feature?)

SourceForge.net noreply at sourceforge.net
Sun Sep 25 15:25:24 CEST 2005


Bugs item #1303928, was opened at 2005-09-25 15:06
Message generated for change (Comment added) made by birkenfeld
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1303928&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Interpreter Core
Group: Python 2.4
>Status: Closed
>Resolution: Invalid
Priority: 5
Submitted By: David M. Beazley (beazley)
Assigned to: Nobody/Anonymous (nobody)
Summary: Extended slice bug (or feature?)

Initial Comment:
Consider a sequence:

a = [1,2,3,4,5,6,7,8,9,10]

Now, consider the following slices:

b = a[:-5]        # b gets [1,2,3,4,5]
c = a[:-5:1]     # c gets [1,2,3,4,5]
d = a[:-5:-1]    # d gets [10,9,8,7]

Is this the intended behavior??  I would have suspected the value of 
d to be [5,4,3,2,1]  (the same elements of c, but in reverse order).

Thanks.  -- Dave


----------------------------------------------------------------------

>Comment By: Reinhold Birkenfeld (birkenfeld)
Date: 2005-09-25 15:25

Message:
Logged In: YES 
user_id=1188172

Closing again ;)

----------------------------------------------------------------------

Comment By: Michael Hudson (mwh)
Date: 2005-09-25 15:17

Message:
Logged In: YES 
user_id=6656

Yes, it's intended.  l[i:j:k][0] == l[i] when it makes sense, and the sense of 
omitted indices flips depending on the sign of k.  It's a bit confusing, but 
then I think all the variants are in some cases.



----------------------------------------------------------------------

Comment By: Reinhold Birkenfeld (birkenfeld)
Date: 2005-09-25 15:16

Message:
Logged In: YES 
user_id=1188172

No. With a step of -1, the slice always starts at the end
(at 10). The stop (that is 6, in this case) is never
included, and so you get your result.

If you want [5,4,3,2,1], use a[:-5][::-1] or reversed(a[:-5]).

PS: comp.lang.python is a better place to discuss whether
something is really a bug.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1303928&group_id=5470


More information about the Python-bugs-list mailing list