Generator Expression vs List Comprehension 1
What are the two main differences between list comprehension and generator expression?
Answer
The syntax for a list comprehension uses [] and the syntax for a generator expression uses ().
Return type: List comprehension returns a list, while a generator expression returns a generator object.
Memory usage / evaluation: List comprehension creates the entire list in memory immediately, whereas a generator expression produces items on the fly (lazy evaluation).
Notes and comments
Comment 1: Note that a generator object is an iterable; more specifically, it is an iterator.