Home βΊ Problems βΊ Wrong function to find list length
Wrong function to find list length
π Fix the bug Topic: lists Difficulty: easy Free
What this problem practises
Lists are mutable and assigned by reference. That one fact explains most list bugs: aliasing between what look like "copies", append() and sort() returning None, and slice boundaries that disagree with the index arithmetic.
This is a fix-the-bug problem: a short program is given in its broken state, and the task is to repair the code so it produces the required output. The interactive editor runs your fixed version in the browser and checks it for you β no setup, no local interpreter.
Difficulty: easy β the fix or the trick is normally a single line, and a careful read should be enough.
The challenge
This should print how many items are in the cart, but crashes. Fix it.
The code runs in PyDebugβs in-browser compiler. Open the problem, run the snippet, watch the exact error or output, and keep iterating until it is right. Signed-in solves count toward XP and your daily streak.
The error it raises
Run the broken snippet and Python stops with this exact error: AttributeError: 'list' object has no attribute 'length'. Match it to the Python errors guide, then repair the line that produced it β knowing the message is half the fix.
The snippet
The interactive editor pre-fills exactly this code, broken and ready to repair:
cart = ["shirt", "shoes", "cap"]
count = cart.length()
print(count)Understand the rule
Python measures containers with the built-in len(items) β there is no items.length attribute and no items.size() method, and reaching for them raises AttributeError: 'list' object has no attribute 'length'. This trips developers arriving from JavaScript (array.length) or Java (list.size()); Python routes the operation through one universal function instead.
How to trace it
- Read the AttributeError: the attribute it names (length, size, count with no args) reveals which language habit wrote the line.
- Replace with the built-in: len(items) works on lists, strings, tuples, dicts, and sets alike.
- If code called items.count(), note that count(x) counts occurrences of x β with no argument it is a TypeError, not a length.
- Re-run; also check comparisons like len(items) > 0, where truthiness (if items:) is the cleaner idiom.
Common wrong fixes
- Writing items.len() β the function wraps the object, not the other way round.
- Storing the length once and reusing it after the list changed size.
- Using len() on a generator, which has no length until consumed; convert to a list first or count while iterating.
Rule to remember
Length in Python is always len(container) β one built-in for every collection type. length and size() belong to other languages; the AttributeError is your translation hint.
π¦ Learning path: Lists and Dictionaries
Problem 12 of 20 in this path. Work them in order β each one builds on the rule before it.
β Previous problemNext in path: Accessing a missing dictionary key β
More lists problems
Keep going
- Python debugging guide β a systematic method for finding bugs like this one fast.
- Browse every lists problem in this topic.
- Common Python errors and what they actually mean, plus the Python glossary for any term used on this page.