When the Division Actually Happens

data = [1, 0, 2]

lst = [10 / x for x in data]
gen = (10 / x for x in data)
print("created gen, no error yet")
list(gen)

Treat these as two separate runs of the same script. What happens in each?

A

The list comprehension prints the message and defers the error, while the generator expression raises immediately, since generators evaluate eagerly to validate their source.

B

The list comprehension raises ZeroDivisionError immediately, while the generator expression prints its message first and only raises ZeroDivisionError once list(gen) consumes it.

C

Both raise ZeroDivisionError immediately at the line that creates them, since Python evaluates comprehensions and generator expressions the same way.

D

Neither raises an error, because both 10 / x for x = 0 and x = 0 in a comprehension are treated as 0 rather than division.