summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
Diffstat (limited to 'Doc')
-rw-r--r--Doc/ref/ref5.tex19
-rw-r--r--Doc/tut/tut.tex21
2 files changed, 35 insertions, 5 deletions
diff --git a/Doc/ref/ref5.tex b/Doc/ref/ref5.tex
index db98fe4..6b699bc 100644
--- a/Doc/ref/ref5.tex
+++ b/Doc/ref/ref5.tex
@@ -152,13 +152,22 @@ A list display is a possibly empty series of expressions enclosed in
square brackets:
\begin{verbatim}
-list_display: "[" [expression_list] "]"
+list_display: "[" [expression_list [list_iter]] "]"
+list_iter: list_for | list_if
+list_for: "for" expression_list "in" testlist [list_iter]
+list_if: "if" test [list_iter]
\end{verbatim}
-A list display yields a new list object. If it has no expression
-list, the list object has no items. Otherwise, the elements of the
-expression list are evaluated from left to right and inserted in the
-list object in that order.
+A list display yields a new list object. Its contents are specified
+by providing either a list of expressions or a list comprehension.
+When a comma-separated list of expressions is supplied, its elements are
+evaluated from left to right and placed into the list object in that
+order. When a list comprehension is supplied, it consists of a
+single expression followed by one or more "for" or "if" clauses. In this
+case, the elements of the new list are those that would be produced
+by considering each of the "for" or "if" clauses a block, nesting from
+left to right, and evaluating the expression to produce a list element
+each time the innermost block is reached.
\obindex{list}
\indexii{empty}{list}
diff --git a/Doc/tut/tut.tex b/Doc/tut/tut.tex
index b53d6ea..f811932 100644
--- a/Doc/tut/tut.tex
+++ b/Doc/tut/tut.tex
@@ -1753,6 +1753,27 @@ item, then to the result and the next item, and so on. For example,
0
\end{verbatim}
+\subsection{List Comprehensions}
+
+List comprehensions provide a concise way to create lists without resorting
+to use of the \func{map()} or \func{filter()} functions. The resulting
+construct tends often to be clearer than use of those functions.
+
+\begin{verbatim}
+>>> spcs = [" Apple", " Banana ", "Coco nut "]
+>>> print [s.strip() for s in spcs]
+['Apple', 'Banana', 'Coco nut']
+>>> vec = [2, 4, 6]
+>>> print [3*x for x in vec]
+[6, 12, 18]
+>>> vec1 = [2, 4, 6]
+>>> vec2 = [4, 3, -9]
+>>> print [x*y for x in vec1 for y in vec2]
+[8, 6, -18, 16, 12, -36, 24, 18, -54]
+>>> print [x+y for x in vec1 for y in vec2]
+[6, 5, -7, 8, 7, -5, 10, 9, -3]
+\end{verbatim}
+
\section{The \keyword{del} statement \label{del}}
There is a way to remove an item from a list given its index instead