How to Get the Shape of a List or Tuple in Python
When working with numerical data, libraries like NumPy provide a convenient .shape attribute to quickly find the dimensions of an array. However, standard Python list and tuple objects do not have this attribute. This is because, unlike NumPy arrays, lists and tuples are not required to have a uniform, rectangular structure.
The "shape" of a list or tuple must be computed by inspecting its structure using the built-in len() function. This guide will show you how to get the shape for both 1D and 2D lists, covering both rectangular (uniform) and jagged (non-uniform) structures.
Understanding "Shape": Lists vs. NumPy Arrays
The reason lists don't have a .shape attribute is fundamental to their design:
- NumPy
ndarray: A homogeneous, fixed-size array stored in a contiguous block of memory. Its dimensions (shape) are a core, stored property. - Python
list: A dynamic, heterogeneous collection of pointers to objects. Its "shape" is not a stored property but rather a result of the lengths of its nested lists, which must be calculated by iterating through them.
Getting the Shape of a 1D List or Tuple
For a simple one-dimensional list or tuple, its "shape" is simply its length, which you can find using the len() function.
Solution:
my_list = [10, 20, 30, 40, 50]
# The "shape" is just the number of elements.
shape = len(my_list)
print(f"The list has {shape} elements.")
Output:
The list has 5 elements.
Getting the Shape of a 2D Rectangular List
A rectangular list is a list of lists where every inner list has the same number of elements. In this case, you can get a shape tuple (rows, columns) that is analogous to NumPy's .shape.
Solution:
# All inner lists have 3 elements.
rectangular_list = [[1, 2, 3], [4, 5, 6]]
# The number of rows is the length of the outer list.
rows = len(rectangular_list)
# The number of columns is the length of any inner list (e.g., the first one).
columns = len(rectangular_list[0]) if rows > 0 else 0
shape = (rows, columns)
print(f"The shape of the list is: {shape}")
Output:
The shape of the list is: (2, 3)
This method assumes all rows have the same length. It will give a misleading column count if your list is jagged. It will also raise an IndexError if the list is empty ([]).
Getting the Shape of a 2D Jagged List
A jagged list is a list of lists where the inner lists can have different lengths. In this case, there is no single "columns" number. The shape must be represented as the number of rows and a list of the column counts for each row.
Solution: a list comprehension is the most Pythonic way to get the length of each inner list.
# The inner lists have different lengths.
jagged_list = [[1, 2, 3], [4], [5, 6]]
# The number of rows is the length of the outer list.
rows = len(jagged_list)
# Use a list comprehension to get the length of each inner list.
columns_per_row = [len(row) for row in jagged_list]
print(f"Number of rows: {rows}")
print(f"Columns per row: {columns_per_row}")
# You can represent the shape as a tuple
shape = (rows, columns_per_row)
print(f"The 'shape' of the jagged list is: {shape}")
Output:
Number of rows: 3
Columns per row: [3, 1, 2]
The 'shape' of the jagged list is: (3, [3, 1, 2])
Conclusion
| Scenario | Goal | Method |
|---|---|---|
| 1D List/Tuple | Get the total number of elements. | len(my_list) |
| 2D Rectangular List | Get the shape as (rows, columns). | (len(my_list), len(my_list[0])) |
| 2D Jagged List | Get the number of rows and the length of each row. | (len(my_list), [len(row) for row in my_list]) |
While Python lists and tuples do not have a built-in .shape attribute like NumPy arrays, you can easily compute their dimensions using the len() function. For 2D lists, you must be mindful of whether your data is rectangular or jagged to choose the correct approach.