How to Get the Length of a List in Python with len
· 3 min read
How many items are in a list? Python's built-in len answers that. It's also how you check whether a list is empty, or find the position of the last item. This guide counts a list of three fruits, with real output from Python 3.15.2.
Syntax
Syntax
len(<list>)
| Part | What it means |
|---|---|
<list> | The list to count. len also works on strings, tuples, dictionaries and other collections |
| The result | A whole number: how many items there are |
The built-in functions reference: "Return the length (the number of items) of an object."
Prerequisites
- uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with
uv init.
Step 1: Write the Script
length.py
fruits = ["apple", "banana", "cherry"]
print(len(fruits))
print(len([]))
![The length.py script in a macOS-style code window: a fruits list of apple, banana and cherry, then print(len(fruits)) and print(len([])).](https://strdewiridedevblogsprod.blob.core.windows.net/images/blog/programming/python/2026/python-len-01-code.webp)
Step 2: Run It
Terminal
uv run python length.py
Output
3
0

Three fruits, and 0 for the empty list [].
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Using the length as the last position | fruits[len(fruits)] raised IndexError: list index out of range: positions run from 0 to 2 | The last item is fruits[len(fruits) - 1], or simply fruits[-1] |
Calling len on a number | len(42) raised TypeError: object of type 'int' has no len() | Turn it into text first if you want its digits: len(str(42)) |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
TypeError: object of type ... has no len() | The value isn't a collection | Print the value and its type(...) to see what it is |
FAQ
How do I check that a list is empty?
len(fruits) == 0 works, and so does not fruits: an empty list counts as false in Python.
Does len work on text?
Yes. For a string it counts characters: len("banana") is 6.
Conclusion
len(fruits) gave 3 and len([]) gave 0. The last position is always one less than the length.
Additional Resources
- len() — the function.
- Common sequence operations —
lenamong the operations every sequence supports.
