summaryrefslogtreecommitdiffstats
path: root/Doc/tut
diff options
context:
space:
mode:
authorSkip Montanaro <skip@pobox.com>2000-08-12 18:09:51 (GMT)
committerSkip Montanaro <skip@pobox.com>2000-08-12 18:09:51 (GMT)
commit803d6e5451cbf0416bfb252eedba36ff2e354fac (patch)
treee37c0cf52e141c341e6b8a64a5d7e7691313df41 /Doc/tut
parentb16b83534d008d45a2917bd2c4fd65010909dff2 (diff)
downloadcpython-803d6e5451cbf0416bfb252eedba36ff2e354fac.zip
cpython-803d6e5451cbf0416bfb252eedba36ff2e354fac.tar.gz
cpython-803d6e5451cbf0416bfb252eedba36ff2e354fac.tar.bz2
list comprehensions. see
http://sourceforge.net/patch/?func=detailpatch&patch_id=100654&group_id=5470 for details.
Diffstat (limited to 'Doc/tut')
-rw-r--r--Doc/tut/tut.tex21
1 files changed, 21 insertions, 0 deletions
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