Skip to content
Justice Tech School

Slicing Rows and Columns

a[rows, cols] works with slices. Use : for all.

Example
a = np.arange(16).reshape(4, 4)
print(a[0])
print(a[:, 1])
print(a[1:3, 1:3])
print(a[::2, ::2])

Result

[0 1 2 3]
[ 1  5  9 13]
[[ 5  6]
 [ 9 10]]
[[ 0  2]
 [ 8 10]]

Exercise

All rows second column.

a[, 1]

Quiz Test

What does a[:, 1] select?

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