Length of Array in Python

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

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.

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.

Boost your career prospects with Scaler Topics Python certification course. Get started now!