Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 07dc918

Browse files
committed
Close bug 480337: Dict used before dicts explained. Added explanation
and examples of the dict() constructor.
1 parent 04e7e0c commit 07dc918

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

Doc/tut/tut.tex

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1843,8 +1843,6 @@ \subsection{List Comprehensions}
18431843
[12, 18]
18441844
>>> [3*x for x in vec if x < 2]
18451845
[]
1846-
>>> [{x: x**2} for x in vec]
1847-
[{2: 4}, {4: 16}, {6: 36}]
18481846
>>> [[x,x**2] for x in vec]
18491847
[[2, 4], [4, 16], [6, 36]]
18501848
>>> [x, x**2 for x in vec] # error - parens required for tuples
@@ -2023,6 +2021,17 @@ \section{Dictionaries \label{dictionaries}}
20232021
1
20242022
\end{verbatim}
20252023

2024+
The \function{dict()} contructor builds dictionaries directly from
2025+
lists of key-value pairs stored as tuples. When the pairs form a
2026+
pattern, list comprehensions can compactly specify the key-value list.
2027+
2028+
\begin{verbatim}
2029+
>>> dict([('sape', 4139), ('guido', 4127), ('jack', 4098)])
2030+
{'sape': 4139, 'jack': 4098, 'guido': 4127}
2031+
>>> dict([(x, x**2) for x in vec]) # use a list comprehension
2032+
{2: 4, 4: 16, 6: 36}
2033+
\end{verbatim}
2034+
20262035

20272036
\section{Looping Techniques \label{loopidioms}}
20282037

0 commit comments

Comments
 (0)