Q8: What are List Comprehensions versus Generator Expressions?
- List Comprehension (
[x for x in data]): Evaluates the entire sequence eagerly, building a full list in heap memory. - Generator Expression (
(x for x in data)): Evaluates elements lazily on-demand, returning an iterator with minimal $O(1)$ memory footprint.
Key Takeaways
- List Comprehension = Eager evaluation ($O(N)$ memory). Generator Expression = Lazy iteration ($O(1)$ memory).
Knowledge Check
Which construct should be preferred when streaming 10 GB of log data line-by-line?