The following list comprehension and generator expression are almost, but
not quite, the same:

[expr for x in iterable]

list(expr for x in iterable)


The difference is in the handling of StopIteration raised inside the expr.
Generator expressions consume them and halt, while comprehensions allow
them to leak out. A simple example:

iterable = [iter([])]
list(next(x) for x in iterable)
=> returns []

But:

[next(x) for x in iterable]
=> raises StopIteration


Has anyone come across this difference in the wild? Was it a problem? Do you
rely on that difference, or is it a nuisance? Has it caused difficulty in
debugging code?

If you had to keep one behaviour, which would you keep?



-- 
Steven

-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to