The standard terminology is to call the three slice arguments start, stop and step
L[start:stop:step] The elements in the returned list are
L[start], L[start + step], L[start + 2*step], L[start + 3*step], ... - step cannot be zero and defaults to 1
- stop must not be understood as a last element, but as an unreachable bound. If
start + k*stepreaches the bound, the item with this index is not in the returned sequence. - If start (or stop) is < 0, python adds the length of the list to its value before doing anything.
- Then if step < 0 but stop >= start, the returned list is empty
- Similarly if step > 0 and stop <= start.
- If stop is empty, it means 'no bound', like in
L[2::1]. - If start is empty, it means start from the first or the last element depending on step > 0 or step < 0
For example
L[::-1] starts from the last element with no bound and step -1, it reverses the list.
Gribouillis
Posting Maven
Moderator
2,786 posts since Jul 2008
Reputation Points: 1,044
Solved Threads: 691