In python, an array is an ordered collection of elements that can be of any data type. In this session, we will learn everything about python array length. We will learn how to find the length of an array using various techniques. so let’s get started.
Find the length of an array in python
To find the length of an array in Python, you can use the len()
function. The len()
function in Python returns the length of an object, which is the number of elements present in an object.
Syntax of python len() function
The syntax for the len()
function in Python is as follows:
len(s)
where, s
is the sequence whose length you want to find. This could be any type of sequence like list, tuple, string, etc. The function returns an integer value representing the length of the sequence.
Now let’s create an array “my_array” and use len() function to calculate the length
my_array = [1, 2, 3, 4, 5,6] length = len(my_array) print(length)
Since we have 6 elements in the array so above code returns 6 as output.
Python array index
In Python, the index of an element in an array is basically the position of the element in the array. The index in python usually starts from 0 and goes till len(array)-1.
Let’s use a for loop to print the index and the corresponding element from the python array my_array.
my_array = [1, 2, 3, 4, 5,6] for i, element in enumerate(my_array): print(f"Element at index {i}: {element}")
The enumerate()
function returns an iterator that produces tuples containing the index and the element at each array.
Maximum length of an array in python
In Python, there is no fixed limit on the maximum length of an array. The array can take n number of elements where the length of an array is limited by the amount of memory available on your system.
However, keep in mind that creating very large lists can consume a significant amount of memory, which could slow down your system or cause other performance issues. If you need to work with very large data sets, then I’ll recommend using specialized data structures like NumPy arrays or Pandas.
Calculate the length of an array in python using numpy
So far we have learned how to check the length of an array using the python len()
function. we can also use the python numpy module to calculate the length of an array in python.
Numpy provides shape
attributes that can be used to calculate the array length. The shape attributes return a tuple representing the dimensions of the array. The first element of the tuple is the length of the array along the first dimension and the second element is the length along the second dimension.
Below is an example of how you can find the length of a NumPy array using the shape
attribute
import numpy as np my_array = np.array([[1, 2, 3], [4, 5, 6]]) shape = my_array.shape print(shape)
the array my_array contains 2 rows and 3 columns so we get (2,3) as output.
Python array length example
In this session, we will explore various ways to use the Python len()
function.
Python 2d array size
Python length function can be extended to calculate the length of a 2d array. To calculate the overall length of an array we need to find the number of rows and columns. The final length will be the multiplication of both. Let’s take the below example.
my_array = [[1, 2, 3], [4, 5, 6]] # Find the row count rows = len(my_array) # Find the column count columns = len(my_array[0]) #calculate the size of the array by multiplying the row count by the column count size = rows * columns print(size)
array my_array has 2 rows and 3 columns which gives a total of 2 * 3 = 6 elements.
Python 3d array size
Python length function can also be used to calculate the length of a 3d array. The overall length will be the multiplication of each layer. Let’s take the below example.
my_array = [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]] # Find the length of the array along each dimension layer_count = len(my_array) row_count = len(my_array[0]) column_count = len(my_array[0][0]) # Calculate the size of the array by multiplying the length along each dimension size = layer_count * row_count * column_count print(size)
This array has 3 “layers”, each of which has 2 rows and 2 columns, for a total of 3 * 2 * 2 = 12 elements.
Check if the array length is greater than zero in python
Sometimes a user needs to check if the length of an array is greater than zero to perform subsequent operations. He can achieve this by combining len()
function with if
statement. Below is an example of how to achieve this:
my_array = [1, 2, 3, 4, 5,6] if len(my_array) > 0: print("The array is not empty") else: print("The array is empty")
since my_array is not empty, so we get “The array is not empty” as output.
Python array length for loop
For loop can be used with len()
function to loop over the elements of an array using their indices. Below is an example of for loop to print all the elements in the array my_array:
my_array = [1, 2, 3, 4, 5,6] # Loop over the indices of the my_array for i in range(len(my_array)): print(my_array[i])
The code prints all the elements in my_aarray separated by a new line.
Declare the length of an array in python
In Python, you can declare an array with a specific length by using the *
operator to repeat a value a certain number of times. This method of array declaration is quite useful when you want the array to contain similar elements.
Below is an example to declare an array with 5 elements, all set to 2.
# Declare an array with 5 elements, all set to 2 my_array = [2] * 5 print(my_array)
Conclusion
Finally, we have come to the end of this tutorial on Python array length. Please do let me know if you need further input.