Which of these expressions creates a generator object rather than a list?
Correct answer A. (x * 2 for x in range(5))
Explanation
Wrapping a comprehension in parentheses creates a generator expression, which yields values lazily one at a time. Square brackets build a full list, braces build a set, and passing a generator to list() consumes it into a list. Generators save memory for large or infinite sequences.
Answer checked How we write and check questions
Discussion