Timeline for answer to How slicing in Python works by Hans Nowak
Current License: CC BY-SA 4.0
Post Revisions
14 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Aug 5, 2025 at 8:56 | comment | added | user29120650 |
@endolith x[::-1][:k] gets the last k items even if k = 0. Unfortunately, it also copies the entire list first, if the python docs are actually correct about their claims. But why do you need the reverse of the last k items?
|
|
| S Nov 20, 2024 at 10:26 | history | rollback | Hans Nowak |
Rollback to Revision 3 - Edit approval overridden by post owner or moderator
|
|
| Nov 4, 2024 at 16:56 | history | suggested | jolammi | CC BY-SA 4.0 |
The ascii diagram was wrongly formatted, fixed to match the linked Python documentation
|
| Oct 31, 2024 at 18:01 | review | Suggested edits | |||
| S Nov 20, 2024 at 10:26 | |||||
| S Nov 17, 2023 at 9:06 | history | suggested | Samuel RIGAUD | CC BY-SA 4.0 |
Fix not aligned indexes + update documentation link
|
| Nov 15, 2023 at 18:21 | review | Suggested edits | |||
| S Nov 17, 2023 at 9:06 | |||||
| Apr 15, 2021 at 1:04 | comment | added | aguadopd | Addendum to my comment: see my answer with diagrams below: stackoverflow.com/a/56332104/2343869 | |
| Apr 5, 2021 at 21:32 | comment | added | Javier Ruiz | @aguadopd You are absolutely right. The solution is to have the indices shifted to the right, centered just below the characters, and notice that the stop is always excluded. See another response just below. | |
| Jul 6, 2019 at 20:07 | comment | added | endolith |
But there's no way to collapse to an empty set starting from the end (like x[:0] does when starting from the beginning), so you have to special-case small arrays. :/
|
|
| May 27, 2019 at 20:05 | comment | added | aguadopd |
This suggestion works for positive stride, but does not for a negative stride. From the diagram, I expect a[-4,-6,-1] to be yP but it is ty. What always work is to think in characters or slots and use indexing as a half-open interval -- right-open if positive stride, left-open if negative stride.
|
|
| Jan 14, 2019 at 2:33 | review | Suggested edits | |||
| Jan 14, 2019 at 6:44 | |||||
| Dec 22, 2017 at 8:05 | review | Suggested edits | |||
| Dec 22, 2017 at 11:11 | |||||
| Sep 18, 2017 at 11:02 | history | edited | kenorb | CC BY-SA 3.0 |
Improves post formatting.
|
| Feb 3, 2009 at 22:49 | history | answered | Hans Nowak | CC BY-SA 2.5 |