Skip to main content
Python & FastAPI Interviews Predict Output P1 Medium

Generator short-circuiting and exhaustion

Predict both printed lines when calling `4 in nums` followed by `list(nums)` on a generator expression.

Official Reference

Code Snippet

nums = (n * 2 for n in [1, 2, 3])
print(4 in nums)
print(list(nums))

Expected Output & Explanation

True
[6]

Generators are single-use iterators. Checking `4 in nums` consumes 2 and 4, then short-circuits and stops as soon as 4 matches—leaving 6 unconsumed, so `list(nums)` returns `[6]`. A second `list(nums)` (or checking `99 in nums`) exhausts it completely and returns `[]`.

Follow-up Interview Question · Why do `any()` and `all()` short-circuit generators the same way, and when is that a big win?

Module Context & References

Python · Predict the output & bug traps Widely asked Python gotchas: defaults, closures, scoping, copies and async
Personal Notes · Markdown

Technologies & Interview Topics Covered

Switch between short-term interview tracks and long-term 80/20 engineering depth to practice JavaScript internals, React rendering and hooks, timed frontend machine coding UI challenges, MeetaPro FG300, Blind 75 & NeetCode 150 DSA patterns, Python & FastAPI APIs, PostgreSQL query optimization & MVCC, Redis caching, Applied AI RAG pipelines & evaluations, and distributed system design.