Software Engineer (SDE)pythonintermediate3 interactive MCQs in app
Generators & Yield
lazy O(1) memory vs O(n) listsingle-pass exhaustion pitfallgenerator expressions vs list comprehensions
Core Concept
Generators (`yield` or `(x for x in seq)`) compute values **lazily one item at a time** in **O(1) memory** instead of materializing an entire list in RAM! Ideal for streaming multi-GB log files, paginated API cursors, and chained data pipelines (`sum(parse(line) for line in open_logs())`).
Worked Example
running `rows = (line for line in open("big.log"))` keeps O(1) RAM per line instead of allocating a 4 GB `[line for line in ...]` list!
Common Pitfall
a generator is SINGLE-PASS (`exhausted after one loop`)—calling `len(gen)` raises `TypeError`, and looping over `gen` a second time silently yields **0 items**!
Recommended Readings & Practice
Practice Generators & Yield with 3 graded MCQs & written feedback.
Take SOARDO’s free 12-question diagnostic to see how strong you are across your target role and unlock adaptive daily missions.