← Back to NumPy Course | Chapter 3: Array Attributes | Lesson 2 of 6

ndim

ndim counts how many dimensions (axes) an array has.

In this page:

  1. ndim
Syntax
python
arr.ndim

ndim

ndim is 1 for a line of values, 2 for a table, 3 for a stack of tables. It always equals the length of the shape tuple. Knowing ndim helps you pick the right indexing and axis arguments.

Note: A single number wrapped by np.array has ndim 0.

Example: ndim

python
import numpy as np

print(np.array(5).ndim)
print(np.array([1, 2, 3]).ndim)
print(np.array([[1, 2], [3, 4]]).ndim)
print(np.zeros((2, 3, 4)).ndim)

# Output:
# 0
# 1
# 2
# 3
Related Topics
Common Mistakes
  1. Confusing ndim with the length of the array
  2. Mixing up dimensions and rows
  3. Forgetting scalars have zero dimensions
Chapter Summary
  • ndim is the number of axes
  • It equals len(shape)
  • Scalars have ndim 0
  • It is an attribute not a method
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.