Use the minus operator to specify an index relative to the end of the array.
Slice from the index 3 from the end to index 1 from the end.
import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7]) print(arr[-3:-1]) |
Utilize the step value to define the increment for the slicing.
Return every other element from index 1 to index 5.
import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7]) print(arr[1:5:2]) |
Return every other element from the entire array.
import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7]) print(arr[::2]) |