diff options
| author | Chris Jerdonek <chris.jerdonek@gmail.com> | 2012-10-16 02:47:32 (GMT) |
|---|---|---|
| committer | Chris Jerdonek <chris.jerdonek@gmail.com> | 2012-10-16 02:47:32 (GMT) |
| commit | c7633e684b91b4bd22bd445180e5fac21368bccf (patch) | |
| tree | 7eb39b886adec33310ab789eef03acefc1f84276 /Doc/tutorial/datastructures.rst | |
| parent | 35eeb30adf4e01fa20cd95da1e4d4f80a4d1cd1d (diff) | |
| parent | 38049bb251b90cfc09fb88d25824d464c630586f (diff) | |
| download | cpython-c7633e684b91b4bd22bd445180e5fac21368bccf.zip cpython-c7633e684b91b4bd22bd445180e5fac21368bccf.tar.gz cpython-c7633e684b91b4bd22bd445180e5fac21368bccf.tar.bz2 | |
Issue #16225: Merge from 3.3: Add additional note to tutorial about looping.
Diffstat (limited to 'Doc/tutorial/datastructures.rst')
| -rw-r--r-- | Doc/tutorial/datastructures.rst | 13 |
1 files changed, 13 insertions, 0 deletions
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: |
