1. Slice Notation
1. Slice Notation
# mostlyMostly used variations
s[start:end]
s[start:]
s[:end]
# step Step-related variations
s[:end:step]
s[start::step]
s[::step]
# makeMake a copy
s[:]
NOTE: If start>=endstart >= end (considering only when step>0), pythonPython will return a empty slice [].
2. Pitfalls
2. Pitfalls
The above part explains the core features on how slice works, and it will work on most occasions. However, there can be pitfalls you should watch out, and this part explains them.
Negative indexes
Negative indexes
The very first thing that confuses pythonPython learners is that indexan index can be negative! Don't panic: negativea negative index means count from backwards.
s[-5:] # startStart at the 5th index from the end of array,
# thus returnsreturning the last 5 elements.
s[:-5] # startStart at index 0, and end until the 5th index from end of array,
# thus returnsreturning s[0:len(s)-5].
Negative step
Negative step
MakeMaking things more confusing is that step can be negative too!
NegativeA negative step means iterate the array backwards: from the end to start, with the end index included, and the start index excluded from the result.
NOTE: when step is negative, the default value for start tois len(s) (while end does not equal to 0, because s[::-1] contains s[0]). For example:
s[::-1] # reversedReversed slice
s[len(s)::-1] # The same as above, reversed slice
s[0:len(s):-1] # emptyEmpty list
Out of range error?
Out of range error?
Be surprised: slice does not raise an IndexError when the index is out of range!
If the index is out of range, pythonPython will try its best to set the index to 0 or len(s) according to the situation. For example:
s[:len(s)+5] # The same as s[:len(s)]
s[-len(s)-5::] # The same as s[0:]
s[len(s)+5::-1] # The same as s[len(s)::-1], and the same as s[::-1]
3. Examples
3. Examples
Let's finish this answer with examples explains, explaining everything we have discussed:
# createCreate our array for demonstration
In [1]: s = [i for i in range(10)]
In [2]: s
Out[2]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
In [3]: s[2:] # fromFrom index 2 to last index
Out[3]: [2, 3, 4, 5, 6, 7, 8, 9]
In [4]: s[:8] # fromFrom index 0 up to index 8
Out[4]: [0, 1, 2, 3, 4, 5, 6, 7]
In [5]: s[4:7] # fromFrom index 4 (included) up to index 7(excluded)
Out[5]: [4, 5, 6]
In [6]: s[:-2] # upUp to second last index (negative index)
Out[6]: [0, 1, 2, 3, 4, 5, 6, 7]
In [7]: s[-2:] # fromFrom second last index (negative index)
Out[7]: [8, 9]
In [8]: s[::-1] # fromFrom last to first in reverse order (negative step)
Out[8]: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
In [9]: s[::-2] # allAll odd numbers in reversed order
Out[9]: [9, 7, 5, 3, 1]
In [11]: s[-2::-2] # allAll even numbers in reversed order
Out[11]: [8, 6, 4, 2, 0]
In [12]: s[3:15] # endEnd is out of range, pythonand Python will set it to len(s).
Out[12]: [3, 4, 5, 6, 7, 8, 9]
In [14]: s[5:1] # startStart > end,end; return empty list
Out[14]: []
In [15]: s[11] # accessAccess index 11 (greater than len(s)) will raise an IndexError
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-15-79ffc22473a3> in <module>()
----> 1 s[11]
IndexError: list index out of range