Problem Formulation and Solution Overview
Method 1: Use List Comprehension
This example uses List Comprehension and sum()
to determine the length of a 2D array. This method works well if the arrays do not contain the same number of elements.
import random two_dims = [random.sample(range(10, 50), 3), random.sample(range(50, 100), 7)] tot_len = sum(len(el) for el in two_dims) print(tot_len)
The first line of the above code imports the random
library. This allows access to random.sample()
to generate a sampling of random integers.
The following line creates a 2D array containing randomly generated integers using random.sample()
. This function is then passed two (2) arguments:
- The
range()
function with a start position (inclusive) and a stop position (exclusive, stop-1). - The number of random integers to generate, 3 and 7, respectively.
This is called twice, and the results save to two_dims
.
A 2D array like the one below would display if output to the terminal at this point. This 2D array contains one array of three (3) elements and another with seven (7) elements.
[[47, 46, 45], [56, 74, 97, 67, 72, 68, 92]] |
On the following line, List Comprehension and sum()
are used to loop through and count each element of the 2D array. The total number of elements in the 2D array is saved to tot_len
and output to the terminal.
10 |
Method 2: Use len()
This example uses len()
to determine the length of a 2D array. This method works only if each array contains the same number of elements.
import random two_dims = [random.sample(range(10, 50), 5), random.sample(range(50, 100), 5)] tot_length = (len(two_dims[0]) * len(two_dims)) print(tot_length)
The first line of the above code imports the random
library. This allows us to generate a random sampling of integers.
The following line creates a 2D array containing randomly generated integers using random.sample()
. This function is passed two (2) arguments:
- The
range()
function with a start position (inclusive) and a stop position (exclusive, stop-1). - The number of random integers to generate, five (5) for each array.
This is called twice, and the results save to two_dims
.
A 2D array like the one below would display if output to the terminal at this point. This 2D array contains two (2) arrays of five (5) elements each.
[[25, 37, 21, 13, 34], [75, 77, 52, 55, 97]] |
The next line determines the length of the first array (5). This value is multiplied by the number of dimensions (2). The results save to tot_length
and output to the terminal.
10 |
Method 3: Use shape()
This example uses the shape()
function from the NumPy library to determine the length of a 2D array. This method returns a tuple.
To run the code below error-free, the NumPy library must be installed. Click here for installation instructions.
import numpy as np two_dims = np.array(np.random.randint(low=10, high=100, size=(2, 5))) tot_tuple = np.shape(two_dims) print(tot_tuple[0] * tot_tuple[1])
The first line in the above code imports the NumPy library. This allows access to the array()
and random.randint()
functions.
The following line calls the array()
function. This function is then passed the random.randint()
function. This function is passed three (3) arguments:
- A low position (inclusive).
- A high position (exclusive).
- A size which is a Tuple containing the number of arrays to generate, and the size of these arrays, resulting in (2, 5)
The results save to two_dims
.
A 2D array like the one below would display if output to the terminal at this point. This 2D array contains two (2) arrays of five (5) elements.
[[38 18 32 76 41] |
The next line calls shape()
and passes the 2D array, two_dims
, as an argument. The results save to tot_tuple
(a Tuple
) and are output to the terminal.
(2, 5) |
To retrieve the length, we multiply the Tuple (2*5) and output the results to the terminal.
10 |
Method 4: Use NumPy size
This example uses size
from the NumPy library to determine the length of a 2D array.
To run the code below error-free, the NumPy library must be installed. Click here for installation instructions.
import numpy as np two_dims = np.array(np.random.randint(low=10, high=100, size=(2, 6))) tot_length = two_dims.size print(tot_length)
The first line in the above code imports the NumPy library. This allows access to the array()
, random.randint()
, and size
functions.
The following line calls the array()
function. This function is then passed the random.randint()
function. This function is passed three (3) arguments:
- A low position (inclusive).
- A high position (exclusive).
- A size which is a Tuple containing the number of arrays to generate, and the size of these arrays, resulting in (2, 6)
The results save to two_dims
.
A 2D array like the one below would display if output to the terminal at this point. This 2D array contains two (2) arrays of five (6) elements.
[[66 85 90 36 78 80] |
The next line calculates the length of the 2D array using size
. The results save to tot_length
and output to the terminal.
12 |
π‘Note: The size
function returns the number of elements along a given axis.
Method 5: Use len() and zip()
This example uses the len()
and zip()
functions to determine the length of a 2D array.
To run the code below error-free, the NumPy library must be installed. Click here for installation instructions.
import numpy as np two_dims = np.array(np.random.randint(low=10, high=100, size=(2, 6))) tot_dims = (len(two_dims) * len(list(zip(*two_dims)))) print(tot_dims)
The first line in the above code imports the NumPy library. This allows access to the array()
and random.randint()
functions.
As outlined in Method 4 above, the following line generates a 2D array each containing six (6) randomly generated integers. These results save to two_dims
.
A 2D array like the one below would display if output to the terminal at this point. This 2D array contains two (2) arrays of five (6) elements.
[[23 71 39 39 36 14] |
The following line calculates:
- The number of dimensions by calling the
len()
function and passing ittwo_dims
created above (len(two_dims)
). The result is 2. - Then,
zip()
andlist
are used to calculate the length of each array (len(list(zip(*two_dims)))
). The result is 6.
The results from both are multiplied and saved to tot_dims.
This is then output to the terminal.
12 |
Summary
This article has provided five (5) ways to get the length of a 2D Array to select the best fit for your coding requirements.
Good Luck & Happy Coding!