summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2002-06-25 15:13:18 (GMT)
committerRaymond Hettinger <python@rcn.com>2002-06-25 15:13:18 (GMT)
commit07dc91800f1b379f2c6a46cc1db1b221f202ea4d (patch)
treeca229dbd7d8d1499b2fe700283b0849b1740709b /Doc
parent04e7e0c60fa1d7edea0a5e39285fd18bbcc4107b (diff)
downloadcpython-07dc91800f1b379f2c6a46cc1db1b221f202ea4d.zip
cpython-07dc91800f1b379f2c6a46cc1db1b221f202ea4d.tar.gz
cpython-07dc91800f1b379f2c6a46cc1db1b221f202ea4d.tar.bz2
Close bug 480337: Dict used before dicts explained. Added explanation
and examples of the dict() constructor.
Diffstat (limited to 'Doc')
-rw-r--r--Doc/tut/tut.tex13
1 files changed, 11 insertions, 2 deletions
diff --git a/Doc/tut/tut.tex b/Doc/tut/tut.tex
index 0dc53ef..0a4c95e 100644
--- a/Doc/tut/tut.tex
+++ b/Doc/tut/tut.tex
@@ -1843,8 +1843,6 @@ parenthesized.
[12, 18]
>>> [3*x for x in vec if x < 2]
[]
->>> [{x: x**2} for x in vec]
-[{2: 4}, {4: 16}, {6: 36}]
>>> [[x,x**2] for x in vec]
[[2, 4], [4, 16], [6, 36]]
>>> [x, x**2 for x in vec] # error - parens required for tuples
@@ -2023,6 +2021,17 @@ Here is a small example using a dictionary:
1
\end{verbatim}
+The \function{dict()} contructor builds dictionaries directly from
+lists of key-value pairs stored as tuples. When the pairs form a
+pattern, list comprehensions can compactly specify the key-value list.
+
+\begin{verbatim}
+>>> dict([('sape', 4139), ('guido', 4127), ('jack', 4098)])
+{'sape': 4139, 'jack': 4098, 'guido': 4127}
+>>> dict([(x, x**2) for x in vec]) # use a list comprehension
+{2: 4, 4: 16, 6: 36}
+\end{verbatim}
+
\section{Looping Techniques \label{loopidioms}}