liveBook · Manning

Generator expression

As explained in Python Workout, Second Edition

A generator expression is Python syntax that lazily produces values from an iteration expression without immediately constructing the resulting collection.

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

How Python Workout, Second Edition covers it

How deep it goes
Explains syntax, memory behavior, iterator production, consumer integration, omitted parentheses, and a modest speed tradeoff.
Examples
Demonstrates squared ranges, string joining, large-file translation, object representations, nested data, and a bounded __iter__ implementation.
What it assumes
Builds on list comprehensions and iteration while contrasting immediate list construction with deferred value production.
What you can do after
Apply generator expressions to produce memory-efficient iterators for built-in consumers and class __iter__ methods.
How it connects
Connects generator expressions with list comprehensions, the iterator protocol, generator functions, sum, and str.join.

Also explained in

Compare how 3 books explain generator expression →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python Workout, Second Edition. The section links point to the parts of the book it draws on.