Skip to main content

How to Get the Length of a List in Python with len

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

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>)
PartWhat it means
<list>The list to count. len also works on strings, tuples, dictionaries and other collections
The resultA 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([])).

Step 2: Run It​

Terminal
uv run python length.py
Output
3
0

The script's output in a terminal: 3, then 0.

Three fruits, and 0 for the empty list [].

Common Mistakes​

MistakeWhat happensDo this instead
Using the length as the last positionfruits[len(fruits)] raised IndexError: list index out of range: positions run from 0 to 2The last item is fruits[len(fruits) - 1], or simply fruits[-1]
Calling len on a numberlen(42) raised TypeError: object of type 'int' has no len()Turn it into text first if you want its digits: len(str(42))

Troubleshooting​

SymptomLikely causeFix
TypeError: object of type ... has no len()The value isn't a collectionPrint 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​