generator expression
A comprehension written in parentheses, which produces its items one at a time on demand instead of building a whole list.
In more detail
sum(x * x for x in range(1_000_000)) never holds a million numbers in memory. Like any generator it can be used only once.
Related terms
- list comprehension — A compact Python expression that builds a new list by looping over items and optionally filtering them.
- generator — A function that can pause and resume, producing a series of values one at a time with yield, each only when it is requested.
- lazy evaluation — Postponing work until its result is actually needed, rather than doing it all in advance.
Taught in
- Comprehensions — Hands-on Python: Complete Video Course & Book
- Sorting, Keys And Lambdas — Hands-on Python: Complete Video Course & Book
- Generators And Iterators — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep
- Comprehensions, Closures And Files Sprint — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep