How do you get the size of a 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 determine the size 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 size of an array in Python?

Use ndim attribute available with the NumPy array as numpy_array_name. ndim to get the number of dimensions. Alternatively, we can use the shape attribute to get the size of each dimension and then use len() function for the number of dimensions.

How many bytes is a 2D array?

For every one dimensional array you have a 24 byte overhead in addition to the space for the data in the array. So your first two lines of code each create an array of 10 pointers - you are right about that - which take 8 bytes each on 64-bit system. This means you are allocating 2 * (24 + 10 * 8) = 208 bytes.