← Quiz: Iterators and Generators: all 10 questions
Quiz: Iterators and Generators · Question 7 of 10
Why can sum(x x for x in range(107)) use much less memory than sum([x x for x in range(107)])?
Why can sum(x * x for x in range(10**7)) use much less memory than sum([x * x for x in range(10**7)])?
- A.Generators use faster arithmetic
- B.Python skips some of the numbers
- C.The generator produces one value at a time instead of building a full list first
- D.range() stores fewer numbers inside a generator
Answer & explanation
Correct answer: The generator produces one value at a time instead of building a full list first
Lazy evaluation means only the current value exists in memory.
Can you get the rest right without peeking?
Take the timed Quiz: Iterators and Generators quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →