Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 799b05b

Browse files
committed
merge
2 parents c994cd2 + 502bf51 commit 799b05b

1 file changed

Lines changed: 11 additions & 11 deletions

File tree

Doc/tutorial/datastructures.rst

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -612,18 +612,18 @@ returns a new sorted list while leaving the source unaltered. ::
612612
orange
613613
pear
614614

615-
To change a sequence you are iterating over while inside the loop (for
616-
example to duplicate certain items), it is recommended that you first make
617-
a copy. Looping over a sequence does not implicitly make a copy. The slice
618-
notation makes this especially convenient::
619-
620-
>>> words = ['cat', 'window', 'defenestrate']
621-
>>> for w in words[:]: # Loop over a slice copy of the entire list.
622-
... if len(w) > 6:
623-
... words.insert(0, w)
615+
It is sometimes tempting to change a list while you are looping over it;
616+
however, it is often simpler and safer to create a new list instead. ::
617+
618+
>>> import math
619+
>>> raw_data = [56.2, float('NaN'), 51.7, 55.3, 52.5, float('NaN'), 47.8]
620+
>>> filtered_data = []
621+
>>> for value in raw_data:
622+
... if not math.isnan(value):
623+
... filtered_data.append(value)
624624
...
625-
>>> words
626-
['defenestrate', 'cat', 'window', 'defenestrate']
625+
>>> filtered_data
626+
[56.2, 51.7, 55.3, 52.5, 47.8]
627627

628628

629629
.. _tut-conditions:

0 commit comments

Comments
 (0)