Find the length of 2d array in Python

Published by user on

The length of 2d array in python is the number of rows in it. Generally 2d array has rows with same number of elements in it.
Consider an example, array = [[10,20,30], [40,50,60]].
Length of array is 2.

Number of rows in 2d array

Use len(arr) to find the number of row from 2d array. To find the number columns use len(arr[0]). Now total number of elements is rows * columns.


Consider a 2d array arr = [[20,30,40], [50,60,70]].
Here len(arr) will return 2.
len(arr[0]) will return 3, as it has three columns.


array = [[100,200,300], [400,500,600]]

#get the number of rows
rows = len(array)

#get the number of columns
cols = len(array[0])

print('Length is', rows)
print('Number of columns', cols)
print('Total number of elements', rows * cols)


Length is 2
Number of columns 3
Total number of elements 6

Find length of 2d array using numpy

To find the length of 2d array we can use numpy.shape. This function returns number of rows and columns.


import numpy as numpy
arr_representation = numpy.array([[60, 70],[80, 90],[100, 200]])
print('Number of (rows,cols)', numpy.shape(arr_representation))


Number of (rows,cols) (3, 2)
Categories: python