Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
9ce9f779
Commit
9ce9f779
authored
Sep 01, 2015
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Improve tutorial suggestion for looping techniques
parent
6a31bb5c
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
11 additions
and
11 deletions
+11
-11
Doc/tutorial/datastructures.rst
Doc/tutorial/datastructures.rst
+11
-11
No files found.
Doc/tutorial/datastructures.rst
View file @
9ce9f779
...
@@ -664,18 +664,18 @@ retrieved at the same time using the :meth:`iteritems` method. ::
...
@@ -664,18 +664,18 @@ retrieved at the same time using the :meth:`iteritems` method. ::
gallahad the pure
gallahad the pure
robin the brave
robin the brave
To change a sequence you are iterating over while inside the loop (for
It is sometimes tempting to change a list while you are looping over it;
example to duplicate certain items), it is recommended that you first make
however, it is often simpler and safer to create a new list instead. ::
a copy. Looping over a sequence does not implicitly make a copy. The slice
notation makes this especially convenient::
>>> import math
>>> raw_data = [56.2, float('NaN'), 51.7, 55.3, 52.5, float('NaN'), 47.8]
>>>
words = ['cat', 'window', 'defenestrate'
]
>>>
filtered_data = [
]
>>> for
w in words[:]: # Loop over a slice copy of the entire list.
>>> for
value in raw_data:
... if
len(w) > 6
:
... if
not math.isnan(value)
:
...
words.insert(0, w
)
...
filtered_data.append(value
)
...
...
>>>
words
>>>
filtered_data
[
'defenestrate', 'cat', 'window', 'defenestrate'
]
[
56.2, 51.7, 55.3, 52.5, 47.8
]
.. _tut-conditions:
.. _tut-conditions:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment