Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk2 min

How to Check the Length of an Array in Python

Python’s len() counts sequence items. For multidimensional NumPy arrays, use size for the total element count and shape for dimension lengths.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For 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:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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.

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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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 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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.