Length of array in python: In this post, we will look at three different types of array lengths in Python.
As we all know, Python does not directly support or offer us with the array data structure. Instead, Python provides us with three different ways to use an Array data structure in this case.
Let us first look over the various methods for creating a Python array.
1)Using len() Function in Python:
Length array python: Python supports the array data structure in the following forms:
- List
- Array module
- Numpy library
We can generate an array using any of the abovementioned types and then work with and modify the data using various functions.
len() function:
Len(array) python: The len() function in Python allows us to determine the total number of elements in an array/object. In other words, it returns the number of elements in the array/object.
Syntax:
len(array)
Let us now look at how to find the length of an array in the Python array types mentioned above.
- Python Programming – NumPy
- In Python, How do you append an Array?
- Create NumPy Array from List, Tuple or List of Lists in Python
Calculating the Length of a Python List
Array length in python: The len() function in Python can be used with a list to retrieve and display the number of elements contained by a list.
Example
Approach:
- Give the list as static input and store it in a variable.
- Calculate the length of the given list using the len() function and store it in another variable.
- Print the given List.
- Print the length of the given list.
- The Exit of the Program.
Below is the implementation:
# Give the list as static input and store it in a variable. gvn_lst = [1, 8, 15, 25, 6, 2] # Calculate the length of the given list using the len() function and # store it in another variable. lst_len = len(gvn_lst) # Print the given List print("The given List = ", gvn_lst) # Print the length of the given list print("The given list's length = ",lst_len)
Output:
The given List = [1, 8, 15, 25, 6, 2] The given list's length = 6
2)Using Array Module
Python length array: The Python Array module allows us to create arrays and manipulate them using the module’s numerous functions. To determine the length of an array, use the len() function.
Example
Approach:
- Import array module using the import keyword.
- Pass the given array as an argument to the array() function and store it in a variable.
- Calculate the length of the given array using the len() function and store it in another variable.
- Print the given Array.
- Print the length of the given Array.
- The Exit of the Program.
Below is the implementation:
# Import array module using the import keyword import array # Pass the given array as an argument to the array() function and store # it in a variable. gvn_arry = array.array('i', [10, 20, 30, 40]) # Calculate the length of the given array using the len() function and # store it in another variable. arry_len = len(gvn_arry) # Print the given Array print("The given Array = ", gvn_arry) # Print the length of the given Array print("The given Array's length = ", arry_len)
Output:
The given Array = array('i', [10, 20, 30, 40]) The given Array's length = 4
3)Using Numpy Module
Python length array: We can use the NumPy module to generate an array that may be used for any mathematical purpose. The len() method determines the number of data values in the NumPy array.
Example
Approach:
- Import numpy module as np using the import keyword.
- Pass some random array size as an argument to the arange() function to create an array of size N with values 0 to N-1.
- Calculate the length of the given array using the len() function and store it in another variable.
- Print the given Array.
- Print the length of the given Array.
- The Exit of the Program.
Below is the implementation:
# Import numpy module as np using the import keyword import numpy as np # Pass some random array size as an argument to the arange() function # to create an array of size N with values 0 to N-1 gvn_arry = np.arange(6) # Calculate the length of the given array using the len() function and # store it in another variable. arry_len = len(gvn_arry) # Print the given Array print("The given Array = ", gvn_arry) # Print the length of the given Array print("The given Array's length = ", arry_len)
Output:
The given Array = [0 1 2 3 4 5] The given Array's length = 6