How to get the length of a 2D Array in Python

Borislav Hadzhiev

Last updated: Apr 8, 2024

Reading timeยท4 min

- Get the length of a 2D Array in Python
- Getting the total number of items in a 2D list
- Get the length of a 2D NumPy array
- Getting the total number of elements in a NumPy 2D array

**To get the length of a 2D Array in Python:**

- Pass the entire array to the
`len()`

function to get the number of rows. - Pass the first array element to the
`len()`

function to get the number of columns. - Multiply the number of rows by the number of columns to get the total.

main.py

`my_arr = [[1, 2, 3], [4, 5, 6]] # โ Get the number of rows rows = len(my_arr) print(rows) # ๐๏ธ 2 # โ Get the number of columns cols = len(my_arr[0]) print(cols) # ๐๏ธ 3 # โ Get the number of total elements total_elements = rows * cols print(total_elements) # ๐๏ธ 6`

The code for this article is available on GitHub

The len() function returns the length (the number of items) of an object.

The argument the function takes may be a sequence (a string, tuple, list, range or bytes) or a collection (a dictionary, set, or frozen set).

We first passed the entire list to the `len()`

function to get the number of
rows (the number of nested lists).

main.py

`my_arr = [[1, 2, 3], [4, 5, 6]] rows = len(my_arr) print(rows) # ๐๏ธ 2 cols = len(my_arr[0]) print(cols) # ๐๏ธ 3`

Assuming that each nested list has the same number of elements, we can get the length of a column by accessing a nested list and passing the result to the

`len()`

function.You can get the total number of items in the 2D list by multiplying the number of rows by the number of columns.

If the nested lists may be of different sizes, use the `sum()`

function.

main.py

`my_arr = [[1, 2, 3], [4, 5, 6, 7, 8]] rows = len(my_arr) print(rows) # ๐๏ธ 2 total_elements = sum(len(l) for l in my_arr) print(total_elements) # ๐๏ธ 8`

The code for this article is available on GitHub

The sum function takes an iterable, sums its items from left to right and returns the total.

We pass each nested list to the `len`

function and calculate the sum.

You can also use a for loop to get the total number of items in a 2D array containing subarrays of different sizes.

main.py

`my_arr = [[1, 2, 3], [4, 5, 6, 7, 8]] total_elements = 0 for subarray in my_arr: total_elements += len(subarray) print(total_elements) # ๐๏ธ 8`

We used a `for`

loop to iterate over the 2D array and on each iteration, we use
the add the length of the current array to the total.

If you need to get the length of a 2D NumPy array, use the `shape`

and `size`

attributes.

main.py

`import numpy as np my_2d_arr = np.array([[1, 2, 3], [4, 5, 6]]) print(my_2d_arr.shape) # ๐๏ธ (2, 3) print(my_2d_arr.size) # ๐๏ธ 6 rows, cols = my_2d_arr.shape total_elements = rows * cols print(total_elements) # ๐๏ธ 6`

The code for this article is available on GitHub

The size attribute returns the number of elements in the NumPy array.

main.py

`import numpy as np my_2d_arr = np.array([[1, 2, 3], [4, 5, 6]]) print(my_2d_arr.size) # ๐๏ธ 6 print(my_2d_arr.shape) # ๐๏ธ (2, 3)`

The shape attribute returns a tuple of the array's dimensions.

The first value in the tuple is the number of rows, and the second is the number of columns in the array.

The two-dimensional array contains 2 nested arrays with 3 elements each.

If you need to get the total number of elements in a NumPy 2D array, multiply the number of rows by the number of columns.

main.py

`import numpy as np my_2d_arr = np.array([[1, 2, 3], [4, 5, 6]]) print(my_2d_arr.shape) # ๐๏ธ (2, 3) rows, cols = my_2d_arr.shape total_elements = rows * cols print(total_elements) # ๐๏ธ 6`

The code for this article is available on GitHub

The array in the example has 2 rows with 3 columns each, so it has a total of 6 elements.

If the nested arrays may be of different sizes, use the `sum()`

function to
calculate the number of total elements.

main.py

`import numpy as np my_2d_arr = np.array([[1, 2, 3], [4, 5, 6, 7]], dtype=object) total_elements = sum(len(arr) for arr in my_2d_arr) print(total_elements) # ๐๏ธ 7`

On each iteration, we use the `len()`

function to get the length of the current
subarray and pass the result to the `sum()`

function.

You can also use a simple `for`

loop to calculate the number of total elements
in a 2D array.

main.py

`import numpy as np my_2d_arr = np.array([[1, 2, 3], [4, 5, 6, 7]], dtype=object) total_elements = 0 for arr in my_2d_arr: total_elements += len(my_2d_arr) print(total_elements) # ๐๏ธ 4`

The code for this article is available on GitHub

On each iteration of the `for`

loop, we add the length of the current array to
the `total_elements`

variable.

I've also written an article on how to check if a value exists in a two-dimensional list.