]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Close bug 480337: Dict used before dicts explained. Added explanation
authorRaymond Hettinger <python@rcn.com>
Tue, 25 Jun 2002 15:02:31 +0000 (15:02 +0000)
committerRaymond Hettinger <python@rcn.com>
Tue, 25 Jun 2002 15:02:31 +0000 (15:02 +0000)
and examples of the dict() constructor.

Doc/tut/tut.tex

index cd8b5c09ee8c87d354c9eb0cd16e2fa9b0a01f4f..f45604b4c135dfce772f9292b754bba8a58f2fc9 100644 (file)
@@ -2023,6 +2023,18 @@ 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{More on Conditions \label{conditions}}
 
 The conditions used in \code{while} and \code{if} statements above can