How to Check How Many Dimensions Arrays Have


In this example we will show how to check how many dimensions the NumPy arrays have in Python.

Source Code

import numpy as np

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

print(i.ndim)
print(j.ndim)
print(k.ndim)
print(n.ndim)

Output:

0
1
2
3
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments