A Python data structure named “array” stores a collection of identical types of data. However, a list can store similar and various types of Python data. It is also referred to as a dynamic array. In Python, finding the length of an array is important for looping, indexing, memory management, and debugging.
In this tutorial, we will provide various ways to find the array length in Python. The listed below contents will be elaborated in depth using multiple examples:
- Method 1: Using len() Function
- Method 2: Using array.size Attributes of Numpy
- Method 3: Using Traditional for Loop Method
- Method 4: Using array.shape Attributes of Numpy
- Method 5: Using length_hint() Function
Method 1: Using len() Function
The “len()” function retrieve the length of the array, as demonstrated in the following code:
Code:
array_value = [5, 25, 55, 45, 8]
print(array_value)
print('Length of Array = ', len(array_value))
- The array named “array_value” is initialized in the program.
- The “len()” function accepts the array as an argument and retrieves the array length.
Output:
The above output depicts that the length of the input array is “5”.
Method 2: Using array.size Attributes of Numpy
The Numpy module provides a size attribute “array.size” to find the array length. Here is an example:
Code:
import numpy
array_value = numpy.array([45, 55, 65, 75])
print ("Given array_valueay: ", array_value)
print ("Length of Array: ", array_value.size)
- The “numpy.array()” function of the numpy module is used to initialize the array.
- The “array_value.size” attribute retrieves the length of the given array.
Output:
The above snippet shows the array length.
Method 3: Using for Loop Method
The traditional “for loop” can also be used to get the array length, as shown in the following code:
Code:
import array
array_value = array.array('i', [55, 45, 60, 80])
counter = 0
for i in array_value:
counter += 1
print("Length of Given Array: ", counter)
- The “array.array()” function of the array module is used to initialize an integer array.
- The “0” value is assigned to the counter variable and the “for” loop is used to iterate over the given array.
- The “counter+=1” statement is used to increment the counter value by “1” for each element value of the array.
- The total element value or the length of the array is stored in a variable “counter”.
Output:
The above snippet shows the given array’s length.
Method 4: Using array.shape Attributes of Numpy
The “array.shape” attribute is used in the following code to find the length of the array:
Code:
import numpy
array_value = numpy.array([51, 22, 60])
print("Length of Array: ", array_value.shape)
- The “Numpy” module function “numpy.array()” is used to create the array of specific size.
- The “array_value.shape” attribute is used to find the length of the given array.
Output:
The above snippet shows the length of the array.
Method 5: Using length_hint() Function
The below code uses the “length_hint()” function to calculate the length of the given array:
Code:
import array
import operator
array_value = array.array('f', [4.5, 5.5, 8.8, 9.9])
print('Length of Array: ', operator.length_hint(array_value))
- The “array.array()” function is used to initialize the floating point array by taking the type code “f” and initializer as an argument.
- The “lenght_hint()” function accepts the array value as an argument and retrieves the length of the array.
Output:
The above snippet shows the array length.
Conclusion
To find the array length the “len() function”, “array.size” attribute, “for loop” method, “array.shape” attributes, and “length_hint()” functions are used in Python. The inbuilt “len()” function and the “length_hint()” functions of the operator module are used to find the array length in Python. Another option is to use a for loop to calculate the number of items in an array. This guide provided various ways to get the length of the array in Python.