Length of Array in Python

Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!

Learn via video course

Python Certification Course: Master the essentials
Python Certification Course: Master the essentials
By Rahul Janghu
Free
star4.90
Enrolled: 1000
Python Certification Course: Master the essentials
Python Certification Course: Master the essentials
Rahul Janghu
Free
4.90
icon_usercirclecheck-01Enrolled: 1000
Start Learning

Overview

We have learned that arrays are a collection of objects, and these collections can have any number of objects: zero, one, ten, and so on. The number of objects in the array is called its length. We need to know the length of an array to perform various operations like iterating over the array, checking whether an element belongs to the array and reading/updating elements in the array.

Scope

  • In this article, we shall see how the length of an array is calculated in Python using default libraries as well as NumPy library.
  • We will not discuss how these library methods internally work to compute the length of an array in Python programming.

Introduction

Let us take an array as follows:

How many elements are in the array? The answer is 5. Hence the length of the array is also 5.

If we were to give indexes to each element in the array as follows:

introduction link

Essentially, the length of an array is the highest index position + 1.

Array Length in Python using the len() method

Python has a len() method to find out the length of an array. The syntax is as follows:

Example

Output

Finding the Length of a Python NumPy Array

Numpy is a library compatible with Python for operating complex mathematical operations on multi-dimensional arrays. We use numpy for mathematical operations on arrays. Here is how we can find the length of an array in Python using numpy:

Example

Output

Conclusion

  • Length of an array is the number of elements present in the array.
  • In python, the length of an array is the highest index of an array + 1.
  • We can use Python's len() function to find the length of an array.
  • For a Numpy array also, we can use the len() function to find the array's length in Python.