Bird
0
0

Why does len() return the number of top-level elements in a nested list, not the total nested elements?

hard📝 Conceptual Q10 of 15
Python - Magic Methods and Operator Overloading
Why does len() return the number of top-level elements in a nested list, not the total nested elements?
ABecause len() counts only the outer list elements, not nested contents
BBecause len() automatically flattens nested lists before counting
CBecause len() counts all elements recursively by default
DBecause len() counts only elements of the first nested list
Step-by-Step Solution
Solution:
  1. Step 1: Understand len() behavior on nested lists

    len() counts how many items are in the outer list, not inside nested lists.
  2. Step 2: Explain why it does not count nested elements

    len() does not look inside nested lists automatically; it treats each nested list as one element.
  3. Final Answer:

    Because len() counts only the outer list elements, not nested contents -> Option A
  4. Quick Check:

    len() counts top-level elements only [OK]
Quick Trick: len() counts outer list items, not nested elements [OK]
Common Mistakes:
  • Thinking len() flattens lists
  • Assuming recursive counting
  • Counting only first nested list
  • Confusing len() with sum()

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes