The length of a Python array or list is len(my_list). It is a built-in function, not a method or a property, so it is len(x), never x.length or x.len(). It works on lists, strings, tuples, dictionaries, sets, and anything else with a defined length, and it runs in constant time - Python stores the count, so len never walks the whole thing. One wrinkle worth clearing up: what most people call a Python array is actually a list, and that distinction occasionally matters.
Table of contents
- len is a function, not a method
- len is O(1), so use it freely
- Empty checks: len versus truthiness
- Lists are not really arrays
- Length of nested and multi-dimensional data
- How this fits the rest of the stack
- FAQ
len is a function, not a method
Coming from JavaScript or Java, the instinct is .length or .size(). Python uses a built-in function:
nums = [10, 20, 30, 40]
len(nums) # 4
# NOT these:
nums.length # AttributeError
nums.len() # AttributeError
len() takes the object as an argument and returns its length. This is a deliberate Python design choice - a single built-in that works across every container type - rather than each type carrying its own length method.
It works on everything with a size:
len("hello") # 5 - characters in a string
len((1, 2, 3)) # 3 - tuple
len({"a": 1, "b": 2}) # 2 - keys in a dict
len({1, 2, 3}) # 3 - set
One function, every container. Once the muscle memory switches from .length to len(...), it applies everywhere.
len is O(1), so use it freely
A performance point that removes a common worry: len() is constant time. Python objects store their length as a field and update it on every change, so len() just reads that field - it never counts elements one by one.
big = list(range(10_000_000))
len(big) # instant, does not walk 10 million items
This means there is no cost to calling len() in a loop condition or repeatedly. You do not need to cache it into a variable for speed:
for i in range(len(items)): # fine, len is cheap
...
That said, in idiomatic Python you rarely need range(len(...)) at all - you iterate the items directly, or use enumerate when you need the index. But if you do call len, do it without worrying about cost. It is one of the cheapest operations in the language.
Empty checks: len versus truthiness
To check whether a list is empty, you have two options, and the idiomatic one is not len:
if len(items) == 0: # works, but not the Python way
...
if not items: # idiomatic - empty sequences are falsy
...
An empty list, string, tuple, dict, or set is falsy, so if not items reads as if there are no items and is the preferred style. Likewise if items means if there are any items.
The len(items) == 0 form is not wrong, and linters accept it, but experienced Python reads if not items. The one time to prefer an explicit len check is when the distinction between empty and, say, None matters - then if not items is ambiguous because None is also falsy, and you may want if items is not None and len(items) == 0. For the common case, truthiness wins.
Lists are not really arrays
The word array in the search hides a real distinction. Python has three different things people call arrays:
list- the everyday[1, 2, 3]. Dynamic, holds mixed types, this is what people mean 95% of the time.lenworks.array.array- the standard-libraryarraymodule, a compact typed array of one numeric type. Rare, used to save memory.lenworks.numpy.ndarray- the numeric powerhouse. For a NumPy array,len(arr)gives the length of the first axis, butarr.shapeandarr.sizeare usually what you want, since arrays are multi-dimensional.
import numpy as np
a = np.zeros((3, 4))
len(a) # 3 - just the first dimension
a.shape # (3, 4)
a.size # 12 - total elements
For a plain list, len is the whole story. For NumPy, len gives only the outer dimension, so reach for .shape or .size when you mean total elements. Knowing which array you have prevents a subtle wrong count on multi-dimensional data.
Length of nested and multi-dimensional data
len on a nested list counts the outer elements, not the total:
grid = [[1, 2, 3], [4, 5, 6]]
len(grid) # 2 - two rows, not six items
len(grid[0]) # 3 - items in the first row
To count everything in a nested structure, sum the inner lengths or flatten first:
total = sum(len(row) for row in grid) # 6
This matters for grids, matrices, and lists of records. len answers how many top-level elements, and for anything deeper you compute it explicitly. The mistake is assuming len(grid) gives the total cell count - it gives the row count, and reaching for sum(len(row) for row in grid) or NumPy’s .size is how you get the real total.
How this fits the rest of the stack
Knowing that len is a constant-time function, and that a list is not the same as a NumPy array, is the kind of small fluency that keeps data-handling code correct under load. When that code runs as a service processing real payloads, the difference between counting rows and counting cells is the difference between a right answer and a plausible wrong one. The RunxBuild hosting calculator lays out the service, database, storage, and bandwidth as separate line items, and the RunxBuild dashboard is where the team watches deploys, logs, and restarts as they happen.
Useful related references:
- What Is the Cheapest Per Month Python Hosting Service? The Honest Answer for 2026
- 500 Internal Server Error: The Version of the Answer That Actually Fixes One
- Change Python Version: The Three Tools That Make It Stop Hurting
- Python services on RunxBuild
FAQ
How do I get the length of an array in Python?
Use the built-in len() function: len(my_list). It is a function, not a method or property, so it is never my_list.length or my_list.len(). It works on lists, strings, tuples, dictionaries, and sets, and returns the number of elements.
Why is it len(x) and not x.length in Python?
Python uses a single built-in len() function that works across all container types, rather than each type carrying its own length method. Writing x.length or x.len() raises AttributeError. It is a deliberate design choice for consistency.
Is len() slow on a large list?
No. len() is constant time. Python stores each object’s length as a field and updates it on every change, so len() reads that field without counting elements. You can call it freely in loops and conditions without any performance concern.
What is the Python way to check if a list is empty?
Use if not items, since empty sequences are falsy. if not items reads as if there are no items and is more idiomatic than if len(items) == 0. Use an explicit len check only when you must distinguish an empty list from None.
Does len() work on a NumPy array?
Yes, but len(arr) returns only the length of the first axis, not the total element count. For a multi-dimensional NumPy array, use arr.shape for the dimensions and arr.size for the total number of elements.