@@ -154,17 +154,20 @@ The :keyword:`for` statement is used to iterate over the elements of a sequence
154
154
(such as a string, tuple or list) or other iterable object:
155
155
156
156
.. productionlist :: python-grammar
157
- for_stmt: "for" `target_list ` "in" `expression_list ` ":" `suite `
157
+ for_stmt: "for" `target_list ` "in" `starred_list ` ":" `suite `
158
158
: ["else" ":" `suite `]
159
159
160
160
The expression list is evaluated once; it should yield an iterable object. An
161
- iterator is created for the result of the ``expression_list ``. The suite is
162
- then executed once for each item provided by the iterator, in the order returned
163
- by the iterator. Each item in turn is assigned to the target list using the
164
- standard rules for assignments (see :ref: `assignment `), and then the suite is
165
- executed. When the items are exhausted (which is immediately when the sequence
166
- is empty or an iterator raises a :exc: `StopIteration ` exception), the suite in
167
- the :keyword: `!else ` clause, if present, is executed, and the loop terminates.
161
+ iterator is created for the result of the ``starred_list ``. The expression
162
+ list can contain starred elements (``*x, *y ``) that will be unpacked in the
163
+ final iterator (as when constructing a ``tuple `` or ``list `` literal). The
164
+ suite is then executed once for each item provided by the iterator, in the
165
+ order returned by the iterator. Each item in turn is assigned to the target
166
+ list using the standard rules for assignments (see :ref: `assignment `), and then
167
+ the suite is executed. When the items are exhausted (which is immediately when
168
+ the sequence is empty or an iterator raises a :exc: `StopIteration ` exception),
169
+ the suite in the :keyword: `!else ` clause, if present, is executed, and the loop
170
+ terminates.
168
171
169
172
.. index ::
170
173
statement: break
@@ -196,6 +199,8 @@ the built-in function :func:`range` returns an iterator of integers suitable to
196
199
emulate the effect of Pascal's ``for i := a to b do ``; e.g., ``list(range(3)) ``
197
200
returns the list ``[0, 1, 2] ``.
198
201
202
+ .. versionchanged :: 3.11
203
+ Starred elements are now allowed in the expression list.
199
204
200
205
.. _try :
201
206
.. _except :
0 commit comments