Python Array Length Tutorial

Python does not provide the array type but provides a similar structure named list . But the array term and structure are very popular in programming and a Python list can be also called a Python array. In this tutorial, we will call the Python list a Python array and examine how to get and work with Python array length.

Array Length with len() Function

The len() function function is used to return the length of the array in Python. This return value is an integer which is the size of the array. But keep in mind that the length of the array is not the same with the indexz number as the index number starts from 0.

names = ["ismail","ahmet","ali"]

numbers = [4,3,2,1]

print("The length of names is ",len(names))

print("The length of numbers is ",len(numbers))
The length of names is  3
The length of numbers is  4

NumPY Array Length with array.size

NumPY is popular Python library which also provides its array type. The NumPY array size can be list using the size attribute of the array .

import numpy as np

a = np.array([1,2,3,4])

print("The lenght of the a is ",a.size)
The lenght of the a is 4

NumPY Multi Dimensional Array Length with array.size

The NumPy size attribute can be also used to print length of a multi dimensional array. In the followin example we create an array which consist of two dimensions or levels.

import numpy as np

a = np.array([[1,6],[2,7],[3,8],[4,9]])

print("The lenght of the a is ",a.size)
The lenght of the a is 8

Leave a Comment