• Post author:
  • Post category:NumPy / Python
  • Post last modified:March 27, 2024
  • Reading time:13 mins read
You are currently viewing How to Get NumPy Array Length

In Python Numpy you can get array length/size using numpy.ndarray.size and numpy.ndarray.shape properties. The size property gets the total number of elements in a NumPy array. The shape property returns a tuple in (x, y).

Advertisements

You can get the length of the multi-dimensional array with the shape property in Python. In this article, we’ll explain several ways of how to get Numpy array length with examples by using properties like numpy.ndarray.size and numpy.ndarray.shape with examples.

1. Quick Examples to Get NumPy Array Length

If you are in a hurry, below are some quick examples of getting Python NumPy array size.


# Below are the quick examples

# Example 1: Use numpy.size Property 
arr = np.array([1,3,6,9,12,15,20,22,25])
result = arr.size

# Example 2: Use numpy.size property 
# To get length of a NumPy array
array = np.array([[1,3,6],[9,12,15],[20,22,25]])
result = array.size

# Example 3: Use numpy.shape property
array = np.array([[1,3,6],[9,12,15],[20,22,25]])
result = array.shape

2. Use NumPy.size to Get Length

You can use ndarray.size property to get the total number of elements (length) in a NumPy array. Let’s create a NumPy array and use this to get the number of elements from one-dimensional arrays.


# Import numpy
import numpy as np

# Create input array
arr = np.array([1,3,6,9,12,15,20,22,25])
print("NumPy array:", arr) 

# Use numpy.size property 
result = arr.size
print("After getting the numpy array length:", result) 

Yields below output.

NumPy array length

In the below code, you get the number of elements in the multi-dimensional array with the ndarray.size property in Python. It also gives us the value 9 because the total number of elements is the same as in the previous example. This is the reason why this method is not suitable for multi-dimensional arrays.


# Create multi-dimensional array
array = np.array([[1,3,6],[9,12,15],[20,22,25]])

# Use numpy.size property 
# To get length of a NumPy array
result = array.size
print("After getting the numpy array length:", result) 

# Output:
# After getting the numpy array length: 9

3. Use numpy.shape to Get Array Length

So for multi-dimensional NumPy arrays use ndarray.shape function which returns a tuple in (x, y), where x is the number of rows and y is the number of columns in the array. You can now find the total number of elements by multiplying the values in the tuple with each other. This method is preferred over the previous method because it gives us the number of rows and columns.


# Create multi-dimensional array
array = np.array([[1,3,6],[9,12,15],[20,22,25]])

# Use numpy.shape property
result = array.shape
print("After getting the numpy shape property:", result) 

# Output:
# After getting the numpy shape property: (3, 3)

Frequently Asked Questions

How can I get the length of a NumPy array?

You can get the length of a NumPy array using the len() function, which returns the size of the first dimension of the array. Alternatively, you can use the size attribute of the NumPy array to get the total number of elements in the array.

Can I use len() with multi-dimensional NumPy arrays?

You can use len() with multi-dimensional NumPy arrays, but it will return the size of the first dimension. If you want to get the size of other dimensions or the total number of elements, you should use the appropriate array attributes like shape or size.

Is there a NumPy function to get the length of an array?

You can use the shape attribute of a NumPy array to get its dimensions. For a 1D array, it gives the length of the array. For multi-dimensional arrays, it returns a tuple representing its shape.

How do I get the length of a specific dimension in a multi-dimensional NumPy array?

You can use the shape attribute of a NumPy array to get the length of each dimension. For example, if arr is a 2D array, arr.shape would return a tuple (rows, columns), indicating the length of each dimension.

How do I find the length of a NumPy array?

You can find the length of a NumPy array using the built-in len() function in Python, which returns the number of elements in the first dimension of the array.

What is the difference between using len() and the size attribute to get the length of a NumPy array?

len() returns the size of the first dimension of the array, while the size attribute gives you the total number of elements in the array. If you have a multi-dimensional array, using size will give you the total count of all elements in the array, not just the size of the first dimension.

Conclusion

In this article, I have explained how to get Python Numpy array length/size or shape using ndarray.shape, ndarray.size properties with examples. By using the size property you can get the size of the array however it is not suitable to get the length of the multi-dimensional array. In order to get the multi-dimensional array size use the shape property which returns the tuple(x,y)

Happy Learning!!

References