@@ -85,24 +85,24 @@ flow control is continued with ``x`` being set to the next entry in the list.
8585After the first matching integer is found, the loop is terminated with the
8686``break `` statement.
8787
88- Loops with an index
89- -------------------
88+ `` for `` Loops with an index
89+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
9090
9191You can also output the index in a ``for `` loop, for example with
9292:py:func: `enumerate() `:
9393
9494.. code-block :: pycon
9595
9696 >>> data_types = ["Data types", "Numbers", "Lists"]
97- >>> for index , title in enumerate(data_types):
98- ... print(index , title)
97+ >>> for key , title in enumerate(data_types):
98+ ... print(key , title)
9999 ...
100100 0 Data types
101101 1 Numbers
102102 2 Lists
103103
104104 List Comprehensions
105- -------------------
105+ ~~~~~~~~~~~~~~~~~~~
106106
107107A list is usually generated as follows:
108108
@@ -153,14 +153,65 @@ appended to the end of the expression:
153153 >>> squares
154154 [16, 25, 36, 49]
155155
156+ Dict Comprehensions
157+ ~~~~~~~~~~~~~~~~~~~
158+
159+ :doc: `../types/sequences-sets/lists ` can be converted into :doc: `../types/dicts `
160+ as follows:
161+
162+ .. code-block :: pycon
163+
164+ >>> toc = {}
165+ >>> for key, title in enumerate(data_types):
166+ ... toc[key] = title
167+ ...
168+ >>> toc
169+ {0: 'Data types', 1: 'Numbers', 2: 'Lists'}
170+
171+ Mit Dict Comprehensions vereinfacht sich dies:
172+
173+ .. code-block :: pycon
174+
175+ >>> toc = {key: value for key, value in enumerate(data_types)}
176+ >>> toc
177+ {0: 'Data types', 1: 'Numbers', 2: 'Lists'}
178+
179+ Das allgemeine Format für Dict Comprehensions ist:
180+
181+ :samp: `{ NEW_DICT } = \{ { KEY } : { VALUE } for { MEMBER } in { ITERABLE } \} `
182+
183+ Change a ``Collection ``
184+ ~~~~~~~~~~~~~~~~~~~~~~~
185+
186+ Modifying a ``collection `` while iterating over it can be difficult. Therefore,
187+ a copy of the ``collection `` is often modified instead:
188+
189+ .. code-block :: pycon
190+
191+ >>> for key, title in data_types.items():
192+ ... if key == 0:
193+ ... del data_types[key]
194+ ...
195+ Traceback (most recent call last):
196+ File "<python-input-2>", line 1, in <module>
197+ for key, title in data_types.items():
198+ ~~~~~~~~~~~~~~~~^^
199+ RuntimeError: dictionary changed size during iteration
200+ >>> for key, title in data_types.copy().items():
201+ ... if key == 0:
202+ ... del data_types[key]
203+ ...
204+ >>> data_types
205+ {1: 'Numbers', 2: 'Lists'}
206+
156207 Checks
157208------
158209
159210* Removes all negative numbers from the list ``x = [ -2, -1, 0, 1, 2, 3] ``.
160211
161212* Which list comprehension would you use to achieve the same result?
162213
163- * How would you count the total number of negative numbers in the list ``[-[ 1,
214+ * How would you count the total number of negative numbers in the list ``[[- 1,
164215 0, 1], [-1, 1, 3], [-2, 0, 2]] ``?
165216
166217* Creates a generator that only returns odd numbers from 1 to 10.
0 commit comments