Python - Find Length of Array


Find Length of Array in Python

In Python, you can find the length of given array using len() built-in function.

In this tutorial, you will learn how to find the length of a given Python array, with examples.

To get the length of a given array my_array in Python, pass the array as argument to the len() built-in function. The len() function returns an integer value representing the number of elements in the given array.

len(my_array)

1. Get length of Integer Array

In the following program, we take an integer array my_array with some initial values, and its length using len().

Python Program

import array as arr

my_array = arr.array('i', [100, 200, 300, 400])
my_array_length = len(my_array)

print("Array length :", my_array_length)

Output

Array length : 4

2. Get length of an Empty Array

In the following program, we take an empty array my_array, and then find its length. Since the array is empty, we should get zero as result.

Python Program

import array as arr

my_array = arr.array('i')
my_array_length = len(my_array)

print("Array length :", my_array_length)

Output

Array length : 0

Summary

In this Python Array tutorial, we learned how to find the length of given array in Python, using len() function, with examples.