summaryrefslogtreecommitdiffstats
path: root/Doc/tutorial/datastructures.rst
diff options
context:
space:
mode:
authorChris Jerdonek <chris.jerdonek@gmail.com>2012-10-16 02:44:47 (GMT)
committerChris Jerdonek <chris.jerdonek@gmail.com>2012-10-16 02:44:47 (GMT)
commit4fab8f0e067772c4c13c86056c2b659cd8e5a812 (patch)
tree3e17477b02bc9af2bf60cef0c02e81766fdff1bb /Doc/tutorial/datastructures.rst
parentf341317185ef7ec6d0cfb2ca93c9d253c3c75305 (diff)
downloadcpython-4fab8f0e067772c4c13c86056c2b659cd8e5a812.zip
cpython-4fab8f0e067772c4c13c86056c2b659cd8e5a812.tar.gz
cpython-4fab8f0e067772c4c13c86056c2b659cd8e5a812.tar.bz2
Issue #16225: Add additional note to tutorial about changing sequence while looping.
Diffstat (limited to 'Doc/tutorial/datastructures.rst')
-rw-r--r--Doc/tutorial/datastructures.rst13
1 files changed, 13 insertions, 0 deletions
diff --git a/Doc/tutorial/datastructures.rst b/Doc/tutorial/datastructures.rst
index 12b5c57..e008dd8 100644
--- a/Doc/tutorial/datastructures.rst
+++ b/Doc/tutorial/datastructures.rst
@@ -584,6 +584,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: