Slicing
Syntax [x:y:z]:
x: start indexy: end index (exclusive)z: take very nth element.- For example, if
z = 1it takes every elements - if
z = 2it takes every 2nd elements - if
zis negative, it will take in reverse
- For example, if
elems = list(range(10))
print(elems[::-1]) # [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
print(elems[::-2]) # [9, 7, 5, 3, 1]