QUESTION 5 OF 15

Why use a generator instead of returning a list?

Reveal the answer and explore the explanation.

05
MediumHigh priority

Why use a generator instead of returning a list?

Answer
Answer

Lazy iterator that yields values on demand rather than storing all of them.

Practical example

Stream batches from a large file with yield.

Reference: Further reading ↗

Last 30 min revisionHigh-yield interview Q&As