Generator short-circuiting and exhaustion
Predict both printed lines when calling `4 in nums` followed by `list(nums)` on a generator expression.
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 `[]`.