17.7 Memory and Performance Characteristics
Memory Efficiency of Generator Expressions Generator expressions are fundamentally different from list, dict, and set comprehensions in their memory usage. While comprehensions create the entire data structure in memory immediately, a generator expression returns an iterator object that produces items one at a time, on demand. This lazy evaluation means it only holds one item in memory at any given moment. This is critically important when working with large or infinite data streams, as it prevents your program from consuming all available RAM.