Slices
The slicing operator s[i:j]
- Extracts all elements s[n] where i <= n < j
a = [0,1,2,3,4,5,6,7,8]
b = a[3:6] # b = [3,4,5]
- If either index is omitted, beginning or end of sequence is assumed
c = a[:3] # c = [0,1,2]
d = a[5:] # d = [5,6,7,8]
- Negative index is taken from the end of the sequence
e = a[2:-2] # e = [2,3,4,5,6]
f = a[-4:] # f = [5,6,7,8]
- No indices just makes a copy (which is sometimes useful)
g = a[:] # g = [0,1,2,3,4,5,6,7,8]
|