How to Slice 2-D NumPy Arrays


In this example we will show how to slice 2-D NumPy Arrays in Python.

Source Code

import numpy as np

n = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])

# from the first element, slice elements from index 1 to index 3 (not included)
print(n[0, 1:3])

# from both elements, slice index 1 to index 3 (not included), this will return a 2-D array
print(n[0:2, 1:3])

# from both elements, return index 3:
print(n[0:2, 3])

Output:

[2 3]
[[2 3]
 [6 7]]
[4 8]
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments