# How to check if a NumPy Array is multidimensional or 1D

Last updated: Apr 12, 2024
3 min

## #How to check if a NumPy Array is multidimensional or 1D

Use the `ndim` attribute on the NumPy array to check if it is multidimensional or one-dimensional.

The `ndim` attribute returns the number of array dimensions:

• greater than 1 if the array is multidimensional.
• equal to 1 if the array is one-dimensional.
main.py
```Copied!```import numpy as np

arr1 = np.array([2, 4, 6, 8])
print(arr1.ndim)  # ๐๏ธ 1

print('-' * 50)

if arr1.ndim == 1:
# ๐๏ธ this runs
print('The array is one-dimensional')
else:
print('The array is multidimensional')

print('-' * 50)

arr2 = np.array([[1, 2, 3], [4, 5, 6]])
print(arr2.ndim)  # ๐๏ธ 2
``````

The ndarray.ndim attribute returns the number of array dimensions.

main.py
```Copied!```import numpy as np

arr1 = np.array([2, 4, 6, 8])
print(arr1.ndim)  # ๐๏ธ 1

arr2 = np.array([[1, 2, 3], [4, 5, 6]])
print(arr2.ndim)  # ๐๏ธ 2
``````

If you need to check if the array is one-dimensional, check if the `ndim` attribute returns `1`.

main.py
```Copied!```import numpy as np

arr1 = np.array([2, 4, 6, 8])
print(arr1.ndim)  # ๐๏ธ 1

print('-' * 50)

if arr1.ndim == 1:
# ๐๏ธ this runs
print('The array is one-dimensional')
else:
print('The array is multidimensional')
``````

If the attribute returns `1`, then the array is one-dimensional.

Otherwise, it is at least two-dimensional.

If you need to check if the array is multidimensional, check if the `ndim` attribute returns a value greater than `1`.

main.py
```Copied!```import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.ndim)  # ๐๏ธ 2

if arr.ndim > 1:
# ๐๏ธ this runs
print('The array is multidimensional')
else:
print('The array is one-dimensional')
``````

You can use the same approach to check if the NumPy array is two-dimensional.

main.py
```Copied!```import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr.ndim)  # ๐๏ธ 2

if arr.ndim == 2:
# ๐๏ธ this runs
print('The array is two-dimensional')
else:
print('The array is NOT two-dimensional')
``````

## #Checking for empty dimensions with `numpy.squeeze()`

If your array might have empty dimensions, use the numpy.squeeze() method before accessing the `ndim` attribute.

main.py
```Copied!```import numpy as np

arr = np.array([[1, 2, 3]])
print(arr.ndim)  # ๐๏ธ 2

arr = np.squeeze(arr)
print(arr.ndim)  # ๐๏ธ 1
``````

The array in the example has an empty dimension.

main.py
```Copied!```import numpy as np

arr = np.array([[1, 2, 3]])

print(arr) # ๐๏ธ [[1 2 3]]

print(arr.ndim)  # ๐๏ธ 2
``````

The `numpy.squeeze` method removes the axes of length one from the supplied array.

main.py
```Copied!```import numpy as np

arr = np.array([[1, 2, 3]])

print(arr)  # ๐๏ธ [[1 2 3]]

print(arr.squeeze()) # ๐๏ธ [1 2 3]
``````

Accessing the `ndim` attribute after calling `squeeze()` produces a different result.

main.py
```Copied!```import numpy as np

arr = np.array([[1, 2, 3]])
print(arr.ndim)  # ๐๏ธ 2

arr = np.squeeze(arr)
print(arr.ndim)  # ๐๏ธ 1
``````