Python Array Length: How To Get The length of an Array in Python?

Get the length of an Array in Python

If you are having difficulties and don’t know how to get the length of an Array in Python, let’s follow this article. We will help you.

Python length of array

In Python, the array is a collection or list of items or elements, and in this article, we discuss the length of arrays. In Python, the length of an array is calculated using functions to return an integer value consisting of the number present in the given array, known as the array length in Python.

The below section will show us how to achieve this. In Python, the array length is mainly calculated using the len() method. I will discuss the len() function in this article and suggest two more methods. Let me prove it.

Get the length of an array in Python

Using the len() function

Syntax:

len(name_array)

In the above syntax, the input is the array name to be calculated. This function returns an integer value equal to the total number of elements in the array passed as an argument to the function.

Code sample:

arrNumber = [1,2,3,7,9]

# Using the len() to get length of Array in Python
lenArr = len(arrNumber)

print('Length of the array is: ', lenArr)

Output:

Length of the array is:  5

The len() function is a built-in Python method that takes an array as an argument and returns the number in the array. The len() method returns the size of an array.

Using the array.size function

Another popular method for calculating the length of an array is the size function in Numpy. Size is a built-in property in Python and returns the size of an array.

Syntax:

name_array.size

Code sample:

import numpy as np

# Initialize a numpy array.
arr = np.array([2, 3, 4, 5, 7])

# Use the array.size() function to get the length of the numpy array.
size = arr.size

print('The length of array is: ', size)

Output:

The length of the array is:  5

The code snippet shows the number of elements in the array with the size property numpy. The size attribute works well with one-dimensional arrays and does not support multi-dimensional arrays.

Using for loop 

We will practice getting the array’s length using the common iteration method. The for loops iterates through every value in an array item to get the length. Let’s see an examples:

Code sample:

arr = [4, 7, 8, 4, 2, 6]
count = 0

for i in arr:
  count = count + 1

print('The length of array is: ', count)

Output:

The length of the array is: 6

Learn more about the usage of for loop python here.

Summary

In this section, we have also seen how we can compute arrays in Python program using len() function, Numpy Array, and for loop with respective examples. We can conclude that length array is a concept for finding allowed or declared length arrays. With the above three ways, I hope you already know how to get the length of an array in Python.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *