This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Extended slice bug (or feature?)
Type: Stage:
Components: Interpreter Core Versions: Python 2.4
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: beazley, georg.brandl, mwh
Priority: normal Keywords:

Created on 2005-09-25 13:06 by beazley, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (5)
msg26394 - (view) Author: David M. Beazley (beazley) Date: 2005-09-25 13:06
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
msg26395 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2005-09-25 13:16
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.
msg26396 - (view) Author: Michael Hudson (mwh) (Python committer) Date: 2005-09-25 13:17
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.

msg26397 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2005-09-25 13:25
Logged In: YES 
user_id=1188172

Closing again ;)
msg26398 - (view) Author: David M. Beazley (beazley) Date: 2005-09-25 15:50
Logged In: YES 
user_id=7557

Thanks for the clarification----just making sure I get this absolutely right 
before committing anything to paper here.  -- Dave.
History
Date User Action Args
2022-04-11 14:56:13adminsetgithub: 42415
2005-09-25 13:06:19beazleycreate