In Python, a 2D array is typically represented as a list of lists (e.g., [[1, 2], [3, 4]]). When finding its "length," there are two common interpretations: the number of rows (sub-lists) or the total number of individual items across all rows. The method you use depends on which of these you need to find and whether your array is rectangular (all rows have the same length) or jagged (rows have different lengths).
This guide will cover how to get both the row count and the total item count for standard Python lists of lists, as well as the highly efficient methods available for NumPy arrays.
Understanding "Length" in a 2D Array
- Number of Rows: This is the number of sub-lists contained within the main list. It is found using
len(my_array). - Total Number of Items: This is the sum of the lengths of all sub-lists. The calculation method depends on the array's structure.
For Standard Python Lists (Lists of Lists)
Getting the Number of Rows
To find the number of rows, simply use the len() function on the outer list.
Solution:
my_array = [[1, 2, 3], [7, 8, 9]]
number_of_rows = len(my_array)
print(f"The 2D list has {number_of_rows} rows.")Output:
The 2D list has 2 rows.Getting the Total Number of Items (Rectangular Array)
If you know your 2D list is rectangular (i.e., every row has the same number of columns), you can get the total number of items by multiplying the number of rows by the number of columns.
Solution:
rectangular_list = [[1, 2, 3], [7, 8, 9]]
rows = len(rectangular_list)
cols = len(rectangular_list[0])
total_items = rows * cols
print(f"Total rows: {rows}")
print(f"Total columns: {cols}")
print(f"Total number of items: {total_items}")Output:
Total rows: 2
Total columns: 3
Total number of items: 6Getting the Total Number of Items (Jagged Array)
For a jagged array (where rows can have different lengths), the safest and most Pythonic way to get the total item count is to sum the length of each row.
Solution:
jagged_list = [
[1, 2],
[7, 8, 9],
[10],
[100, 101, 102],
[200]
]
total_items = sum(len(row) for row in jagged_list)
print(f"Total number of items in the jagged list: {total_items}")Output:
Total number of items in the jagged list: 10This approach works perfectly for both rectangular and jagged lists, making it the most robust method.
For NumPy Arrays
If you are doing numerical computing, you should use the NumPy library, which provides highly efficient array objects.
Getting Shape and Size (Rectangular Array)
For rectangular NumPy arrays, you can get dimension information and the total item count directly from the .shape and .size attributes.
Solution:
import numpy as np
np_array = np.array([[1, 2, 3], [4, 5, 6]])
rows, cols = np_array.shape
total_items = np_array.size
print(f"Total rows: {rows}")
print(f"Total columns: {cols}")
print(f"Total number of items (size): {total_items}")Output:
Total rows: 2
Total columns: 3
Total number of items (size): 6Handling Jagged NumPy Arrays
If you create a jagged array in NumPy, it creates an array of object dtype where each element is a list. The .shape and .size attributes will not give you the total item count in this case.
Solution: you must fall back to the same method used for standard Python lists.
import numpy as np
np_jagged_array = np.array([[1, 2], [4, 5, 6]], dtype=object)
print(f"Shape of jagged array: {np_jagged_array.shape}") # (2,)
print(f"Size of jagged array: {np_jagged_array.size}") # 2
total_items = sum(len(row) for row in np_jagged_array)
print(f"Total number of items in the jagged NumPy array: {total_items}")Output:
Shape of jagged array: (2,)
Size of jagged array: 2
Total number of items in the jagged NumPy array: 5Conclusion
| Scenario | Goal | Method | Example |
|---|---|---|---|
| Standard List | Get # of rows | len() on the outer list |
len(my_list) |
| Rectangular List | Get total items | rows * columns |
len(my_list) * len(my_list[0]) |
| Jagged List | Get total items | sum() with a generator |
sum(len(row) for row in my_list) |
| NumPy Rectangular | Get dimensions/size | .shape and .size attributes |
my_array.shape, my_array.size |
| NumPy Jagged | Get total items | sum() with a generator |
sum(len(row) for row in my_array) |
For standard Python lists, using sum(len(row) for row in my_list) is the most robust way to find the total number of items, as it works for all cases. For NumPy, the .shape and .size attributes are highly efficient for rectangular arrays.