Built with Tutify.io

    AIStudyLabs

    How to Get Array Length in Python

    How to Get Array Length in Python

    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:

    <a id="h-1-quick-examples-of-getting-length-of-an-array"></a>

    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.

    Related: You can use len() function to get the length of the list.

    
    # 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)
    

    Yields below output.

    <a id="h-3-get-length-of-an-array-using-array-module"></a>

    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

    To calculate the length of an array in Python, you can use a for loop. First, create an array using array() function and set the length to '0'. Then, apply for loop over an array and for each iteration, increment the loop by 1 and increase the length value. Finally, we can get the length of an array.

    
    # 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
    

    <a id="h-4-get-python-array-length-using-numpy-module"></a>

    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.

    Related: In Python, you can convert list to NumPy array.

    
    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.

    <a id="h-4-1-get-multi-dimensional-array-length"></a>

    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!!

    <a id="h-related-articles"></a>

    Related Articles