How to Get the Length of a List in Python
If you want to count how many items are in a Python list, use len().
This page shows the quickest way to do that, explains what len() returns, and points out common beginner mistakes. If you want a broader explanation of the function, see Python len() Function Explained.
Quick answer
numbers = [10, 20, 30, 40]
count = len(numbers)
print(count) # 4
Use len(list_name) to get the number of items in a list.
What this page helps you do
- Get the number of items in a list
- Use the built-in
len()function - Understand what
len()returns - Avoid common beginner mistakes
Use len() to count list items
To get the length of a list, pass the list into len():
numbers = [10, 20, 30, 40]
print(len(numbers))
Output:
4
What happens here:
numbersis a list with 4 elementslen(numbers)returns an integer- That integer is the number of items in the list
You can print the result directly or store it in a variable:
numbers = [10, 20, 30, 40]
count = len(numbers)
print(count)
If you want more detail about how len() works with different Python objects, read Python list length with len() or Python len() Function Explained.
Example with a list of strings
Each item in a list counts as one element.
fruits = ["apple", "banana", "orange"]
count = len(fruits)
print(count)
Output:
3
Explanation:
- The list has 3 string items
len(fruits)returns3- It does not matter how long each word is
- Each list item counts as one item
Empty lists
An empty list has length 0.
items = []
print(len(items))
Output:
0
You can also call len() directly on an empty list:
print(len([]))
Output:
0
This is useful when checking whether a list has items before you try to access them or loop through them.
Length after changing a list
If the list changes, the length changes too. Call len() again to get the current length.
After adding an item
numbers = [10, 20, 30]
print(len(numbers)) # 3
numbers.append(40)
print(len(numbers)) # 4
After removing an item
numbers = [10, 20, 30, 40]
print(len(numbers)) # 4
numbers.pop()
print(len(numbers)) # 3
So:
- Adding an item increases the length
- Removing an item decreases the length
len()gives the current number of items, not an old saved value
If you are working with lists often, how to loop through a list in Python is a helpful next step.
Common mistakes
Here are some common problems beginners run into.
Using my_list.len() instead of len(my_list)
This is wrong:
my_list = [1, 2, 3]
print(my_list.len())
Use this instead:
my_list = [1, 2, 3]
print(len(my_list))
len() is a built-in function. It is not a method on the list object.
Confusing the last index with the length
If a list has 3 items, its length is 3, but the last index is 2.
letters = ["a", "b", "c"]
print(len(letters)) # 3
print(letters[2]) # c
Why? Because list indexing starts at 0.
Using len(my_list) as an index
This causes an error:
letters = ["a", "b", "c"]
print(letters[len(letters)])
len(letters) is 3, but valid indexes are 0, 1, and 2.
If you want the last item, use:
letters = ["a", "b", "c"]
print(letters[-1])
If you get an indexing problem, see IndexError: list index out of range fix explained.
Calling len() on a variable that is not a list
Check what the variable actually contains:
my_list = [1, 2, 3]
print(my_list)
print(type(my_list))
print(len(my_list))
print(my_list[0])
print(my_list[-1])
Useful debugging checks:
print(my_list)
print(type(my_list))
print(len(my_list))
print(my_list[0])
print(my_list[-1])
These help you confirm:
- the variable is really a list
- the list has the number of items you expect
- the first and last items are what you think they are
Also remember that len() works on many container types, not just lists.
When to use this vs the len() reference page
Use this page when you want to solve one task quickly: get the number of items in a list.
Use the reference page when you want to understand len() more generally, including how it works with:
- strings
- tuples
- dictionaries
- sets
- other Python objects
For that, read Python len() Function Explained.
FAQ
How do I count items in a list in Python?
Use len(your_list). It returns the number of items in the list.
Does len() work on an empty list?
Yes. It returns 0.
Is the length the same as the last index?
No. The length is the number of items. The last index is length minus 1.
Can I use len() on things other than lists?
Yes. It also works with strings, tuples, dictionaries, sets, and more.