mirror of
https://github.com/python/cpython.git
synced 2025-12-09 10:37:17 +00:00
Issue #16225: Backport from 3.2: Add additional note to tutorial about looping.
This commit is contained in:
parent
fd6d3b149f
commit
0cffd6be56
2 changed files with 24 additions and 11 deletions
|
|
@ -645,6 +645,19 @@ retrieved at the same time using the :meth:`iteritems` method. ::
|
|||
gallahad the pure
|
||||
robin the brave
|
||||
|
||||
To change a sequence you are iterating over while inside the loop (for
|
||||
example to duplicate certain items), it is recommended that you first make
|
||||
a copy. Looping over a sequence does not implicitly make a copy. The slice
|
||||
notation makes this especially convenient::
|
||||
|
||||
>>> words = ['cat', 'window', 'defenestrate']
|
||||
>>> for w in words[:]: # Loop over a slice copy of the entire list.
|
||||
... if len(w) > 6:
|
||||
... words.insert(0, w)
|
||||
...
|
||||
>>> words
|
||||
['defenestrate', 'cat', 'window', 'defenestrate']
|
||||
|
||||
|
||||
.. _tut-conditions:
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue