How to get the length of an array in Python? You can get the length of an array using the built-in len() function. The array is a collection of items of the same type and can be accessed using a zero-based index.
Python language doesn’t have a built-in array data type but you can use the list, array module, and the NumPy module to represent the arrays.
In this article, I will explain how to get the length of an array in Python using the len() function,as well as the array and numpy module.
Here are the ways to get the length of an array in Python:
Python List – A list is a built-in data type in Python that can be used to represent arrays. Lists are very flexible and can contain elements of different data types.
Python Array module – The array module is a built-in module in Python that provides a way to represent arrays of a specific data type.
Python NumPy array – NumPy is a popular third-party library for scientific computing in Python. It provides a powerful N-dimensional array object that can be used to represent arrays.
1. Quick Examples of Getting Length of an Array
If you are in a hurry, below are some quick examples of how to get the length of an array.
# Quick examples of how to get length of an array
# Initialize the array I
arr = [1,3,6,9,12,15,20,22,25]
# Example 1: Get the length of an array using len()
arr = [1,3,6,9,12,15,20,22,25]
print(“Length of an array:”, len(arr))
# Example 2: Get array length using array module
import array as arr
arr1 = arr.array(‘i’, [1,3,6,9,12,15,20,22,25])
print(“Get the length of array: “,len(arr1))
Example 3: Use numpy.size Property to ge the length of
# Import numpy as np
arr = np.array([1,3,6,9,12,15,20,22,25])
print(“Length of an array:”, arr.size)
# Example 4: Use numpy.size property to get length of
# multi-dimensional array
arr = np.array([[1,3,6],[9,12,15],[20,22,25]])
print(“Get the length of Multi-Dimensional array:”, arr.size)
2. Get the Length of Array using len() Function in Python
Python len() is a built-in function, which is used to get the total number of elements present in the given array. It returns a count of the index that is one more than the number of elements present in the array.
Syntax of len() Function:
# Syntax of len() function
len(array)
To find the total number of elements in an array, you can use the len() function. Simply pass the array as an argument and the function will return the length of the array.
# Get the length of an array using len()
arr = [1,3,6,9,12,15,20,22,25]
print(“Array is:”, arr)
print(“Length of an array:”, len(arr))
Yields below output.
2.1 Get Length of 2-D Array in Python
You can also use the Python len() function to compute the length of a 2-D array. Before going to calculate the length of the 2-D array we need to find the number of rows and columns. The length of the 2-D array is the multiplication of a number of rows and columns. Let’s take an example.
# Get the length of 2-D array
arr = [[1,3,6],[9,12,15],[20,22,25]]
print(“Array is: “,arr)
rows = len(arr)
print(“Total number of rows:”, rows)
columns = len(arr[0])
print(“Total number of columns:”, columns)
print(“Get the length of 2-D array: “, rows*columns)
h
Yields below output.
3. Get Length of an Array using Array Module
In Python, you can use the array module to provide a array() function that creates an array object, which is similar to a list but more efficient for certain types of data. This built-in module provides a way to represent arrays of a specific data type.
# Get array length using array module
import array as arr
arr1 = arr.array(‘i’, [1,3,6,9,12,15,20,22,25])
print(“Array is: “,arr1)
print(“Get the length of array: “,len(arr1))
Yields below output.
3.1 Get Array Length Using For Loop with Array Module
Calculating the length of an array in Python can be done using a simple method. All you need to do is run a for loop on the array and keep track of the increment in a variable. Check out the code below for a clearer understanding:
First, let’s try to find the length of a list using the for aloop. Note that the variable ‘length’ is initialized to 0. Its value increases as the loop goes through each array element (list).
# Get length of an array using for loop
import array as arr
arr1 = arr.array(‘i’, [1,3,6,9,12,15,20,22,25])
print(“Array is: “, arr1)
length = 0
for a in arr1:
length += 1
print(“The length of the array: “, length)
# Output:
# Array is: array(‘i’, [1, 3, 6, 9, 12, 15, 20, 22, 25])
# The length of the array: 9
4. Get Python Array Length using numpy Module
You can use array.size property to get the total number of elements (length) in an array. NumPy is a popular third-party library for scientific computing in Python. It provides a powerful N-dimensional array object that can be used to represent arrays.
Let’s create an array and use an array.size the property to get the number of elements from one-dimensional arrays.
import numpy as np
# Use numpy.size Property to ge the length of
# NumPy array
arr = np.array([1,3,6,9,12,15,20,22,25])
print(“Array is:”, arr)
print(“Length of an array:”, arr.size)
The output is same as the above.
4.1 Get Multi-Dimensional Array Length
You can also get the number of elements in the multi-dimensional array using the array.size property of numpy module. Simply apply .size the property over the 2-D array, and it will return the total number of elements in the array.
# Use numpy.size property to get length of
# multi-dimensional array
arr = np.array([[1,3,6],[9,12,15],[20,22,25]])
print(“Multi-Dimensional array:n”, arr)
print(“Get the length of Multi-Dimensional array:”, arr.size)
# Output:
# Multi-Dimensional array:
[
6. Conclusion
In this article, I have explained how to get array length using different ways in Python. Because it has no built-in array type so that, by representing the len() function, array module, and NumPy module how you can get the length of an array.
Happy Learning!!
How to get the length of an array in Python? You can get the length of an array using the built-in len() function. The array is a collection of items of the same type and can be accessed using a zero-based index. Python language doesn’t have a built-in array data type but you can use Read More Python, Python Tutorial, Pending review by admin, Python Array Examples