List Length with len()
Learn how to count list items with len(), handle empty lists, and avoid confusing length with indexes.
Sounds useful?
Jump into the interactive version.
No setup. No account. Just Python.
List Length with len()
Learn how to count list items with len(), handle empty lists, and avoid confusing length with indexes.
Jump into the interactive version.
No setup. No account. Just Python.
len()
Lists can hold multiple values, but sooner or later you need to know how many values are actually inside. This Block teaches you how to use len() with lists, store the result in a variable, print friendly count messages, and use the length in simple decisions.
You will also learn one very important beginner trap: list length is not the same thing as the last valid index. A list with 3 items has length 3, but its last valid index is 2. Tiny detail, big difference.
What you will learn:
len(my_list) to count how many items are in a list0if and elselen(my_list) - 1 for non-empty listsBy the end, you will be able to measure lists clearly and safely, without manually counting items like a tired spreadsheet worker.
Ready to practice this in the browser?
> Play now