This enhancement extends the syntax of Python 1.5.2 to include what
are known in some other languages as list comprehensions. Here are
some examples:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] fruit = ["Apple", "Banana", "Pear"]mult3 = [3 * x for x in numbers]evens = [x for x in numbers if x % 2 == 0]crossprod = [(x, y) for x in numbers for y in fruits]
http://www.cosc.canterbury.ac.nz/~greg/python/listcomp/download.html.
The semantics are such that the statementatom: '[' testlist [list_iter] ']'list_iter: list_for | list_iflist_for: 'for' exprlist 'in' testlistlist_if: 'if' test
is equivalent tox = [e for v1 in s1 for v2 in s2 ... if t ...]
x = [] for v1 in s1: for v2 in s2: ... if t: ... x.append(e)