PC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minuteFor a Python list or standard-library array.array, call len(array) to get the number of items. With NumPy, the right expression depends on what you mean by length: len(a) counts the first dimension, while a.size counts all elements.
Get the number of items in a Python list or array
Python’s built-in len() returns the number of items in an object. For a list, that means the number of items directly contained in the list, not a count of nested values. The Python 3.12.15 built-in functions documentation defines len() as returning an object’s length, or number of items.
As an Amazon Associate I earn from qualifying purchases.
values = [10, 20, 30]
print(len(values)) # 3
The standard-library array.array is a mutable sequence, so len() works the same way:
from array import array
values = array('i', [10, 20, 30])
print(len(values)) # 3
Here, i selects the array’s integer type code; the result of len(values) is the number of stored items. The Python array module documentation describes array.array as a sequence type.
#1 Best Overall
Choose the right length expression for NumPy
For a one-dimensional NumPy array, len(a) and a.size give the same element count. For a multidimensional array, they answer different questions: len(a) returns the length of the first dimension, while a.size returns the total number of elements across all dimensions.
import numpy as np
a = np.array([[1, 2, 3], [4, 5, 6]])
print(len(a)) # 2: rows, or length of the first dimension
print(a.size) # 6: total elements
print(a.shape) # (2, 3)
The NumPy v2.0 reference for ndarray.size defines it as the number of elements, equal to the product of the dimensions in a.shape. For example, a NumPy array with shape (3, 5, 2) has 30 elements.
Rank #2
Use a.shape[axis] to get the length along a particular dimension, and a.ndim to find how many dimensions the array has. The NumPy v2.3 ndarray reference documents these array attributes.
Free tools Windows power users keep installed
One-click scans. No signup required.
Understand what a nested list’s length counts
len() does not recursively count values inside nested lists. For example:
rows = [[1, 2], [3, 4], [5, 6]]
print(len(rows)) # 3: the outer list has three items
The result is 3 because the outer list contains three row lists. There are six values inside those rows, but obtaining that total requires counting the nested contents separately. For a regular rectangular list, one possible expression is sum(len(row) for row in rows); this assumes each outer item is itself a sequence whose length should be included.
Length is not the same as storage in bytes
Item counts and memory-related byte counts are different measurements. NumPy’s itemsize is the byte length of one element; nbytes is the total number of bytes occupied by the array’s elements. For Python’s array.array, itemsize likewise means bytes per item, not the number of items. See the NumPy ndarray reference and Python array module documentation for those attribute definitions.
Quick Recap
Best Value
Quick guide: which expression should you use?
| Object or question | Expression | What it counts |
|---|---|---|
Python list or array.array |
len(a) |
Items directly in the sequence |
| One-dimensional NumPy ndarray | len(a) or a.size |
Elements in the array |
| Multidimensional NumPy ndarray: first dimension | len(a) or a.shape[0] |
Length of the first axis |
| Multidimensional NumPy ndarray: all elements | a.size |
Product of all dimension lengths |
| NumPy ndarray: a particular dimension | a.shape[axis] |
Length along the selected axis |
| Bytes used by NumPy array elements | a.nbytes |
Total element-storage bytes, not an item count |
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.
Do these 3 things before closing this tab:
1Clear out junk files and repair common Windows errors2Fix the driver behind crashes, sound loss and screen glitches3Repair Windows errors before they cause bigger problems




