summaryrefslogtreecommitdiffstats
path: root/Doc/tutorial
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2015-09-01 09:33:20 (GMT)
committerRaymond Hettinger <python@rcn.com>2015-09-01 09:33:20 (GMT)
commit799b05b0527d6a8c24b453d683e38a37f774d61b (patch)
tree6cea8bf9974e97d3402abf0006875c04d22ed497 /Doc/tutorial
parentc994cd21de90e48b5c636fed55e13ffaacbde231 (diff)
parent502bf511b3bc9668b619d3c4af064bd463395d03 (diff)
downloadcpython-799b05b0527d6a8c24b453d683e38a37f774d61b.zip
cpython-799b05b0527d6a8c24b453d683e38a37f774d61b.tar.gz
cpython-799b05b0527d6a8c24b453d683e38a37f774d61b.tar.bz2
merge
Diffstat (limited to 'Doc/tutorial')
-rw-r--r--Doc/tutorial/datastructures.rst22
1 files changed, 11 insertions, 11 deletions
diff --git a/Doc/tutorial/datastructures.rst b/Doc/tutorial/datastructures.rst
index a2031ed..0d51480 100644
--- a/Doc/tutorial/datastructures.rst
+++ b/Doc/tutorial/datastructures.rst
@@ -612,18 +612,18 @@ 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)
+It is sometimes tempting to change a list while you are looping over it;
+however, it is often simpler and safer to create a new list instead. ::
+
+ >>> import math
+ >>> raw_data = [56.2, float('NaN'), 51.7, 55.3, 52.5, float('NaN'), 47.8]
+ >>> filtered_data = []
+ >>> for value in raw_data:
+ ... if not math.isnan(value):
+ ... filtered_data.append(value)
...
- >>> words
- ['defenestrate', 'cat', 'window', 'defenestrate']
+ >>> filtered_data
+ [56.2, 51.7, 55.3, 52.5, 47.8]
.. _tut-conditions: