Curriculum
Course: NumPy
Login

Curriculum

NumPy

Text lesson

Negative Slicing

Use the minus operator to specify an index relative to the end of the array.

Example

Slice from the index 3 from the end to index 1 from the end.

import numpy as np

arr = np.array([1234567])

print(arr[-3:-1])

STEP

Utilize the step value to define the increment for the slicing.

Example

Return every other element from index 1 to index 5.

import numpy as np

arr = np.array([1234567])

print(arr[1:5:2])

Example

Return every other element from the entire array.

import numpy as np

arr = np.array([1234567])

print(arr[::2])