How to get length of 2d array python

You can get the number of dimensions, shape (length of each dimension), and size (number of all elements) of the NumPy array with ndim, shape, and size attributes of numpy.ndarray. The built-in function len() returns the size of the first dimension.

  • Number of dimensions of the NumPy array: ndim
  • Shape of the NumPy array: shape
  • Size of the NumPy array: size
  • Size of the first dimension of the NumPy array: len()

Use the following one- to three-dimensional arrays as examples.

import numpy as np

a_1d = np.arange(3)
print(a_1d)
# [0 1 2]

a_2d = np.arange(12).reshape((3, 4))
print(a_2d)
# [[ 0  1  2  3]
#  [ 4  5  6  7]
#  [ 8  9 10 11]]

a_3d = np.arange(24).reshape((2, 3, 4))
print(a_3d)
# [[[ 0  1  2  3]
#   [ 4  5  6  7]
#   [ 8  9 10 11]]
# 
#  [[12 13 14 15]
#   [16 17 18 19]
#   [20 21 22 23]]]

Number of dimensions of the NumPy array: ndim

You can get the number of dimensions of the NumPy array as an integer value int with the ndim attribute of numpy.ndarray.

print(a_1d.ndim)
# 1

print(type(a_1d.ndim))
# 

print(a_2d.ndim)
# 2

print(a_3d.ndim)
# 3

If you want to add a new dimension, use numpy.newaxis or numpy.expand_dims(). See the following article for details.

  • NumPy: Add new dimensions to ndarray (np.newaxis, np.expand_dims)

Shape of the NumPy array: shape

You can get the shape (= length of each dimension) of the NumPy array as a tuple with the shape attribute of numpy.ndarray.

Even in the case of a one-dimensional array, it is a tuple with one element instead of an integer value. Note that a tuple with one element has a trailing comma.

  • A tuple with one element requires a comma in Python

print(a_1d.shape)
# (3,)

print(type(a_1d.shape))
# 

print(a_2d.shape)
# (3, 4)

print(a_3d.shape)
# (2, 3, 4)

For example, in the case of a two-dimensional array, shape is (number of rows, number of columns). If you only want to get either the number of rows or columns, you can get each element of the tuple.

print(a_2d.shape[0])
# 3

print(a_2d.shape[1])
# 4

You can also unpack and assign them to different variables.

  • Unpack a tuple and list in Python

row, col = a_2d.shape
print(row)
# 3

print(col)
# 4

Use reshape() to convert the shape. See the following article for details.

  • NumPy: How to use reshape() and the meaning of -1

Size of the NumPy array: size

You can get the size (= total number of elements) of the NumPy array with the size attribute of numpy.ndarray.

print(a_1d.size)
# 3

print(type(a_1d.size))
# 

print(a_2d.size)
# 12

print(a_3d.size)
# 24

Size of the first dimension of the NumPy array: len()

len() is the Python built-in function that returns the number of elements in a list or the number of characters in a string.

  • How to use len() in Python

For numpy.ndarray, len() returns the size of the first dimension. Equivalent to shape[0] and also equal to size only for one-dimensional arrays.

print(len(a_1d))
# 3

print(a_1d.shape[0])
# 3

print(a_1d.size)
# 3

print(len(a_2d))
# 3

print(a_2d.shape[0])
# 3

print(len(a_3d))
# 2

print(a_3d.shape[0])
# 2

How do you find the length of a 2D array?

We use arrayname. length to determine the number of rows in a 2D array because the length of a 2D array is equal to the number of rows it has. The number of columns may vary row to row, which is why the number of rows is used as the length of the 2D array.

How do you find the length of an array in Python?

To find the length of an array in Python, we can use the len() function. It is a built-in Python method that takes an array as an argument and returns the number of elements in the array. The len() function returns the size of an array.

How can you tell the size of a 2D list?

Use len() to find the length of a 2D list in Python. Call len(obj) with a 2D list as obj to get the number of rows in the array. Call len(obj) with a list row as obj to get the number of columns. Multiply the number of rows by that of columns to get the total length of the list.

How do you find the total number of elements in a 2D array?

The total number of elements that can be stored in a multidimensional array can be calculated by multiplying the size of all the dimensions. For example: The array int x[10][20] can store total (10*20) = 200 elements. Similarly array int x[5][10][20] can store total (5*10*20) = 1000 elements.