-
-
Notifications
You must be signed in to change notification settings - Fork 32.3k
bpo-30826: Improve control flow examples #15407
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,20 +66,20 @@ they appear in the sequence. For example (no pun intended): | |
window 6 | ||
defenestrate 12 | ||
|
||
If you need to modify the sequence you are iterating over while inside the loop | ||
(for example to duplicate selected items), it is recommended that you first | ||
make a copy. Iterating over a sequence does not implicitly make a copy. The | ||
slice notation makes this especially convenient:: | ||
|
||
>>> 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'] | ||
|
||
With ``for w in words:``, the example would attempt to create an infinite list, | ||
inserting ``defenestrate`` over and over again. | ||
Code that modifies a collection while iterating over that same collection can | ||
be tricky to get right. Instead, it is usually more straight-forward to loop | ||
over a copy of the collection or to create a new collection:: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The three lines are clearer and more correct in giving both strategies. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're happy with the proposed wording, correct? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. There is no checkbox for "(*) Approve either as is as a definite and acceptable improvement over the status quo or after possible further improvements based on my comments -- you decide." In this case, you considered each suggestion and and gave a short coherent reason why not, one which I can consider when writing or reviewing other doc patches. I appreciate you tackling some of these lingering doc issues which have seemingly been blocked by the quest for perfection even when improvement is clearly possible. |
||
|
||
# Strategy: Iterate over a copy | ||
for user, status in users.copy().items(): | ||
terryjreedy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if status == 'inactive': | ||
del users[user] | ||
|
||
# Strategy: Create a new collection | ||
active_users = {} | ||
for user, status in users.items(): | ||
if status == 'active': | ||
active_users[user] = status | ||
terryjreedy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
.. _tut-range: | ||
|
Uh oh!
There was an error while loading. Please reload this page.