summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
authorChris Jerdonek <chris.jerdonek@gmail.com>2012-10-16 02:46:34 (GMT)
committerChris Jerdonek <chris.jerdonek@gmail.com>2012-10-16 02:46:34 (GMT)
commit38049bb251b90cfc09fb88d25824d464c630586f (patch)
treeb84d4d49863492ad5264c51dfaee61be2b29bc25 /Doc
parent39b867000b8eec1fb13f0eaff909eb9c9c971902 (diff)
parent4fab8f0e067772c4c13c86056c2b659cd8e5a812 (diff)
downloadcpython-38049bb251b90cfc09fb88d25824d464c630586f.zip
cpython-38049bb251b90cfc09fb88d25824d464c630586f.tar.gz
cpython-38049bb251b90cfc09fb88d25824d464c630586f.tar.bz2
Issue #16225: Merge from 3.2: Add additional note to tutorial about looping.
Diffstat (limited to 'Doc')
-rw-r--r--Doc/tutorial/controlflow.rst22
-rw-r--r--Doc/tutorial/datastructures.rst13
2 files changed, 24 insertions, 11 deletions
diff --git a/Doc/tutorial/controlflow.rst b/Doc/tutorial/controlflow.rst
index 1564e96..574f0d0 100644
--- a/Doc/tutorial/controlflow.rst
+++ b/Doc/tutorial/controlflow.rst
@@ -58,24 +58,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']
diff --git a/Doc/tutorial/datastructures.rst b/Doc/tutorial/datastructures.rst
index bd661e2..36abc9c 100644
--- a/Doc/tutorial/datastructures.rst
+++ b/Doc/tutorial/datastructures.rst
@@ -589,6 +589,19 @@ returns a new sorted list while leaving the source unaltered. ::
orange
pear
+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: