- publishing free software manuals
An Introduction to Python
by Guido van Rossum and Fred L. Drake, Jr.
Paperback (6"x9"), 124 pages
ISBN 0954161769
RRP £12.95 ($19.95)

Sales of this book support the Python Software Foundation! Get a printed copy>>>

9.11 Generator Expressions

Some simple generators can be coded succinctly as expressions using a syntax similar to list comprehensions but with parentheses instead of square brackets. These expressions are designed for situations where the generator is used right away by an enclosing function. Generator expressions are more compact but less versatile than full generator definitions and tend to be more memory friendly than equivalent list comprehensions.

Examples:

    >>> sum(i*i for i in range(10))     # sum of squares
    285
    
    >>> xvec = [10, 20, 30]
    >>> yvec = [7, 5, 3]
    >>> sum(x*y for x,y in zip(xvec, yvec)) # dot product
    260
    
    >>> from math import pi, sin
    >>> sine_table = dict((x, sin(x*pi/180)) for x in
     range(0, 91))
    
    >>> unique_words = set(word  for line in page  for word
     in line.split())
    
    >>> valedictorian = max((student.gpa, student.name) for
     student in graduates)
    
    >>> data = 'golf'
    >>> list(data[i] for i in range(len(data)-1,-1,-1))
    ['f', 'l', 'o', 'g']
ISBN 0954161769An Introduction to PythonSee the print edition