How to Calculate the Size of an Array in Python | Python Tutorial

Array Length in Python Along With Examples

Sanjay Priyadarshi

--

Photo by Alex Knight on Unsplash

An array is a built-in data type into programming languages like C++ and Java.

In python, there is no built-in data type called an array. You can use third-party libraries and create arrays.

By the size of the array, I mean how many total numbers of elements are present in the array.

Here we will discuss how we can calculate the length of an array in python.

Python array length

In Python, an array is a collection of elements that are stored in a contiguous memory location.

The index starts from 0, not 1.

To find the length of the array in python, we’ll use the len() method.

The len() method will help us find the length of the array.

You need to keep one thing in mind while calculating the length of the array. Since the array index starts at 0 and not 1, the array’s length is one more than the highest index value of an array.

Syntax of len() method

len(name_of_your_array)

The len() method above takes a parameter that will either be a list or an array.

An example to use the len() and calculate the size of the array

our_array = [9,8,7,6,5]

print("This is our array: ", our_array)

size = len(our_array)

print("The size of our array is: " , size)


#Result:
This is our array: [9, 8, 7, 6, 5]
The size of our array is: 5

The size of the array is always one more than the highest array index.

If the highest array index of an array is 7, then the size of the array will equal 8.

In the example above, the highest array index of the array our_array is 6 and this is why the size of the array our_array is 5.

Multi-dimensional array shape

You can’t use the len() method on a multidimensional array to get a full picture of that array.

--

--

Sanjay Priyadarshi

Join a Community of People Who Love Javascript, Programming and Technology — codertoentrepreneurs.substack.com