liveBook · Manning

Generator expression

As explained in Python Workout: 50 ten-minute exercises

A generator expression is Python expression syntax that lazily produces successive values from an iterable instead of constructing the entire result collection immediately.

When a transformation may produce many values, a list comprehension builds the whole result immediately. A generator expression uses the same expression, loop, and optional filter structure but returns values one at a time, delaying computation and reducing memory use. section 7.2 section 7.17

Use generator expressions when:

  • A consuming function such as sum or str.join can request values from an iterator. section 7.2
  • The input is large enough that materializing a list would consume substantial memory. section 7.17
  • You want to transform or filter values without first storing every result. section 7.7
  • You need an iterator to return from an iterable object's __iter__ method. section 10.7

Summing transformed values

These forms produce the same sum, but the first constructs a list while the latter two produce values lazily: sum([x*x for x in range(100000)]) sum((x*x for x in range(100000))) sum(x*x for x in range(100000)) When a generator expression is the only argument to a function call, its extra parentheses can be omitted. section 7.2

Processing a file

A file can be translated word by word and joined without building a list of all translated words: def plfile(filename): return ' '.join( plword(one_word) for one_line in open(filename) for one_word in one_line.split() ) The nested loops supply words to the generator, while str.join consumes the resulting strings. The approach may trade a little speed for substantially lower memory use on large input. section 7.17

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))

Inspect a generator expression returned by __iter__; it yields indexed values from data for a fixed number of iterations. section 10.7

How Python Workout: 50 ten-minute exercises covers it

How deep it goes
Explains syntax, deferred production, memory trade-offs, nested iteration, and iterator usage, but does not detail generator-expression internals.
Examples
Demonstrates numeric summation, string joining, large-file translation, object representations, nested data traversal, and custom iteration.
What it assumes
Assumes familiarity with comprehensions and for loops, while connecting the feature to iterables and the iterator protocol.
What you can do after
Apply it via Python expressions and built-in consumers to transform, filter, join, sum, and iterate over data with lower memory use.
How it connects
Connects generator expressions to list comprehensions, iterators, generator functions, the iterator protocol, 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: 50 ten-minute exercises. The section links point to the parts of the book it draws on.