How to Get Length of a List in Python
What you’ll build or solve
You’ll get the number of items in a Python list and use that number in real code.
When this approach works best
Getting the length of a list works well when you:
Learn Python on Mimo
- Validate input, like checking a list of results before accessing
items[0]. - Control loops, like stopping after
Nitems or chunking data into batches. - Compare lists, like confirming two lists have the same number of items before pairing them.
Skip this approach if you only need to loop over items. A for item in items loop doesn’t need the length and is usually easier to read.
Prerequisites
- Python 3 installed
- You know what a list is
Step-by-step instructions
1) Get the length with len()
Use len() to get the number of items in a list.
nums= [10,25,30]
count=len(nums)
print(count)
What to look for: len([]) returns 0, so empty lists won’t crash your code.
2) Use the length to check for an empty list
A quick length check helps you avoid IndexError when accessing elements.
Option A (most common): Use the list directly in a condition
items= []
ifitems:
first=items[0]
else:
first=None
print(first)
Option B: Compare len() to 0 for clarity
items= []
iflen(items)>0:
first=items[0]
else:
first=None
print(first)
What to look for: if items: is the standard Python pattern. Empty lists are treated as False.
3) Use the length safely with indexes
Valid indexes stop at len(items) - 1.
names= ["Amina","Luka","Noor"]
last_index=len(names)-1
last_name=names[last_index]
print(last_name)
What to look for: If the list might be empty, len(names) - 1 becomes -1. That’s a valid index in Python, but it means “last item,” so check for emptiness first when that matters.
Examples you can copy
Example 1: Guard before accessing the first item
Bash
results= ["match_1","match_2"]
ifresults:
print("First result:",results[0])
else:
print("No results")
Example 2: Take the last N items (or fewer)
items= ["a","b","c","d","e"]
n=3
start=max(len(items)-n,0)
tail=items[start:]
print(tail)
Example 3: Process a list in batches of size N
items=list(range(10))
batch_size=4
forstartinrange(0,len(items),batch_size):
batch=items[start:start+batch_size]
print(batch)
Example 4: Check two lists have the same length before zipping
names= ["Amina","Luka","Noor"]
scores= [42,38,50]
iflen(names)!=len(scores):
raiseValueError("Lists must have the same length")
pairs=list(zip(names,scores))
print(pairs)
Example 5: Count items that match a condition
len() counts all items in a list, not items that match a rule. Filter first, then measure.
scores= [10,0,25,30]
non_zero_count=len([sforsinscoresifs!=0])
print(non_zero_count)
Common mistakes and how to fix them
Mistake 1: Off-by-one indexing
What you might do:
names= ["Amina","Luka","Noor"]
last=names[len(names)]
Why it breaks: Valid indexes stop at len(names) - 1, so len(names) is out of range.
Correct approach:
names= ["Amina","Luka","Noor"]
last=names[len(names)-1]
print(last)
Or use negative indexing:
names= ["Amina","Luka","Noor"]
last=names[-1]
print(last)
Mistake 2: Using length checks where you don’t need them
What you might do:
items= ["a","b","c"]
foriinrange(len(items)):
print(items[i])
Why it breaks: Nothing crashes, but the code is harder to read and easier to mess up than a direct loop.
Correct approach:
CSS
items= ["a","b","c"]
foriteminitems:
print(item)
Mistake 3: Assuming len() tells you how many items match a rule
What you might do:
scores= [10,0,25,30]
count=len(scores!=0)
Why it breaks: scores != 0 does not filter the list and does not return something you can measure.
Correct approach:
scores= [10,0,25,30]
count=len([sforsinscoresifs!=0])
print(count)
Troubleshooting
If you see TypeError: object of type ... has no len(), the variable isn’t a list or another sized container. Print type(x) to confirm what you’re working with.
If you see IndexError: list index out of range, you likely used len(items) instead of len(items) - 1.
If your code returns an unexpected result for the last item, check for empty lists and watch out for -1 indexing.
If your loop feels complicated, try for item in items and avoid indexes unless you truly need them.
Quick recap
- Use
len(my_list)to get the number of items. len([])is0, so empty lists are safe to measure.- Valid indexes stop at
len(my_list) - 1. - Use
if my_list:to check for emptiness. - Filter first if you want the count of items that match a condition.
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot