Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
0cffd6be
Commit
0cffd6be
authored
Oct 15, 2012
by
Chris Jerdonek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #16225: Backport from 3.2: Add additional note to tutorial about looping.
parent
fd6d3b14
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
24 additions
and
11 deletions
+24
-11
Doc/tutorial/controlflow.rst
Doc/tutorial/controlflow.rst
+11
-11
Doc/tutorial/datastructures.rst
Doc/tutorial/datastructures.rst
+13
-0
No files found.
Doc/tutorial/controlflow.rst
View file @
0cffd6be
...
...
@@ -59,24 +59,24 @@ they appear in the sequence. For example (no pun intended):
::
>>> # Measure some strings:
...
a
= ['cat', 'window', 'defenestrate']
>>> for
x in a
:
... print
x, len(x
)
...
words
= ['cat', 'window', 'defenestrate']
>>> for
w in words
:
... print
w, len(w
)
...
cat 3
window 6
defenestrate 12
It is not safe to modify the sequence being iterated over in the loop (this can
only happen for mutable sequence types, such as lists). If you need to modify
the list you are iterating over (for example, to duplicate selected items) you
must iterate over a copy. The slice notation makes this particularly
convenient::
If you need to modify the sequence you are iterating over while inside the loop
(for example to duplicate selected items), it is recommended that you first
make a copy. Iterating over a sequence does not implicitly make a copy. The
slice notation makes this especially convenient::
>>> for x in a[:]: # make a slice copy of the entire list
... if len(x) > 6: a.insert(0, x)
>>> for w in words[:]: # Loop over a slice copy of the entire list.
... if len(w) > 6:
... words.insert(0, w)
...
>>>
a
>>>
words
['defenestrate', 'cat', 'window', 'defenestrate']
...
...
Doc/tutorial/datastructures.rst
View file @
0cffd6be
...
...
@@ -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:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment