Skip to content
Justice Tech School

Indexing and Slicing

Use [i], [i, j] and slices start:stop:step. Negative indexes count from the end.

Example
a = np.arange(10)
print(a[2], a[-1], a[2:5], a[::3], a[::-1])
b = np.arange(9).reshape(3, 3)
print(b[1, 2], b[:, 0], b[1:, 1:])

Result

2 9 [2 3 4] [0 3 6 9] [9 8 7 6 5 4 3 2 1 0]
5 [0 3 6] [[4 5]
 [7 8]]

Exercise

Last item.

a[]

Quiz Test

What is a[::-1]?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.