🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← 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)])?

  1. A.Generators use faster arithmetic
  2. B.Python skips some of the numbers
  3. C.The generator produces one value at a time instead of building a full list first
  4. 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 →