A generator expression is Python syntax resembling a list comprehension but using round parentheses. Instead of constructing a complete list, it returns a generator that supplies one value at a time when an iterator consumer requests it. section 8.3 section 11.4
Use generator expressions when a consuming operation can process values incrementally:
- Replace square brackets with parentheses to avoid materializing the entire result. section 8.3
- Pass the expression to consumers such as sum or str.join; inside a function call, the outer parentheses can usually be omitted. section 8.3 section 8.15
- Expect the same computed result as a list comprehension while using less memory, with a possible small speed tradeoff. section 8.15
- Remember that the expression produces an iterator, not a reusable list; choose a list comprehension when an immediate collection is required. section 11.4 section 8.3
Sum values lazily
Compare sum([x*x for x in range(100000)]) with sum(x*x for x in range(100000)). Both calculate the same total, but the generator expression lets sum request one squared value at a time. For text conversion, use ','.join(str(number) for number in numbers), because join requires strings rather than integers. section 8.3 section 8.3
Process a large file
A Pig Latin file processor can join transformed words without first storing every word: ' '.join(plword(one_word) for one_line in open(filename) for one_word in one_line.split()). The same expression can be written with explicit outer parentheses, but the function-call form is more compact. section 8.15
class Circle():
def __init__(self, data, max_times):
self.data = data
self.max_times = max_times
def __iter__(self):
n = len(self.data)
return (self.data[x % n]
for x in range(self.max_times))
A generator expression can implement __iter__ directly when a class needs to return a bounded sequence of values. section 11.11


