A comprehension is an expression that builds a list, a set or a dictionary from a sequence: [e for x in t] is the list of the values of the expression e for each element x of t, in order; [e for x in t if c] keeps only the elements for which the condition c is true. With braces, {e for x in t} builds a set and {k: v for x in t} a dictionary.
示例
Example 2.8 (The books of a member, in one line)
The function books_of of the town listing is the set comprehension {loan["book_id"] for loan in loans if loan["member_id"] == member}: for each loan, if it is the member’s, put its book in the set.