Timeline for answer to How slicing in Python works by Beni Cherniavsky-Paskin
Current License: CC BY-SA 4.0
Post Revisions
9 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Mar 17, 2025 at 16:01 | history | edited | Beni Cherniavsky-Paskin | CC BY-SA 4.0 |
added 1026 characters in body
|
| Mar 13, 2025 at 15:11 | comment | added | Beni Cherniavsky-Paskin |
If you're implementing a custom __getitem__(self, key) yourself, you don't have to handle all that. slice objects have a .indices(L) method doing all these adjustments for you, and since range() itself is now cheap and indexable you can simplify it to just: for i in range(L)[key]: ....
|
|
| Mar 13, 2025 at 14:59 | history | edited | Beni Cherniavsky-Paskin | CC BY-SA 4.0 |
small python-2-ism: range() no longer == a list
|
| Jan 2, 2019 at 16:46 | history | edited | Peter Mortensen | CC BY-SA 4.0 |
Active reading.
|
| Oct 30, 2016 at 12:42 | history | edited | Beni Cherniavsky-Paskin | CC BY-SA 3.0 |
temp fix(?) for clipping bug with step>1
|
| Oct 30, 2016 at 12:36 | comment | added | Beni Cherniavsky-Paskin |
@Eastsun Oops, you're right! A clearer case: range(4)[-200:200:3] == [0, 3] but list(this_is_how_slicing_works([0, 1, 2, 3], -200, 200, 3)) == [2]. My if 0 <= i < len(seq): was an attempt to implement "never go outside the sequence" simply but is wrong for step>1. I'll rewrite it later today (with tests).
|
|
| Oct 29, 2016 at 12:56 | comment | added | Eastsun |
The this_is_how_slicing_works is not the same as python slice. E.G. [0, 1, 2][-5:3:3] will get [0] in python, but list(this_is_how_slicing_works([0, 1, 2], -5, 3, 3)) get [1].
|
|
| Mar 29, 2012 at 10:58 | history | edited | Beni Cherniavsky-Paskin | CC BY-SA 3.0 |
defaults of omitted start/stop
|
| Mar 29, 2012 at 10:15 | history | answered | Beni Cherniavsky-Paskin | CC BY-SA 3.0 |