[issue30826] More details in reference 'Looping through a list in Python and modifying it'

2020-09-03 Thread Roundup Robot


Change by Roundup Robot :


--
nosy: +python-dev
nosy_count: 5.0 -> 6.0
pull_requests: +21165
pull_request: https://github.com/python/cpython/pull/22078

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue30826] More details in reference 'Looping through a list in Python and modifying it'

2019-08-23 Thread Raymond Hettinger


Raymond Hettinger  added the comment:


New changeset b6341e676af2f58f3ad9b51a0d2fb7db5a3428e3 by Raymond Hettinger 
(Miss Islington (bot)) in branch '3.8':
bpo-30826: Improve control flow examples (GH-15407) (GH-15410)
https://github.com/python/cpython/commit/b6341e676af2f58f3ad9b51a0d2fb7db5a3428e3


--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue30826] More details in reference 'Looping through a list in Python and modifying it'

2019-08-23 Thread Raymond Hettinger


Change by Raymond Hettinger :


--
resolution:  -> fixed
stage: patch review -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue30826] More details in reference 'Looping through a list in Python and modifying it'

2019-08-23 Thread miss-islington


Change by miss-islington :


--
pull_requests: +15115
pull_request: https://github.com/python/cpython/pull/15410

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue30826] More details in reference 'Looping through a list in Python and modifying it'

2019-08-23 Thread Raymond Hettinger


Raymond Hettinger  added the comment:


New changeset 6fcb6cfb139ade1aac6dbee0b18ca72b18cbe0d2 by Raymond Hettinger in 
branch 'master':
bpo-30826: Improve control flow examples (GH-15407)
https://github.com/python/cpython/commit/6fcb6cfb139ade1aac6dbee0b18ca72b18cbe0d2


--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue30826] More details in reference 'Looping through a list in Python and modifying it'

2019-08-22 Thread Raymond Hettinger


Change by Raymond Hettinger :


--
keywords: +patch
pull_requests: +15112
stage:  -> patch review
pull_request: https://github.com/python/cpython/pull/15407

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue30826] More details in reference 'Looping through a list in Python and modifying it'

2017-09-24 Thread Terry J. Reedy

Terry J. Reedy added the comment:

I agree that the tutorial For section needs be updated to include 
non-sequences. A dict example will help with that.

I agree that the unrealistic insert mis-directs attention and like Raymond's 
replacement.  ['users.copy()' should be 'users.copy().items']

--
versions: +Python 3.7

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue30826] More details in reference 'Looping through a list in Python and modifying it'

2017-09-24 Thread Raymond Hettinger

Raymond Hettinger added the comment:

After looking at this again, I think the entire example should be removed.  We 
really don't want to encourage people to code like this (it would never make it 
through a code review).  The example itself is silly (not fully, just weird and 
lacking real-world motiviation).  The s.insert(0,x) code is an anti-pattern.  
And in general, mutating a data structure while iterating over it is a perilous 
practice leading to fragile code (many data structures ban the practice 
outright: databases, deques, dicts).

Mutating while iterating is only safe if a data structure makes explicit 
guarantees about how it iterates.  In Python, we have only a handful of such 
guarantees (you can safely mutate dict values while iterating over the keys and 
lists guarantee that the iterator looks-up consecutive indicies regardless of 
changes to the underlying list).

I propose to remove the last two paragraphs and the example, replacing them 
with clear practical advice and patterns that would pass a code review.

Something like this:

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.

# Strategy:  Iterate over a copy
for user, status in users.copy():
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

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue30826] More details in reference 'Looping through a list in Python and modifying it'

2017-07-12 Thread R. David Murray

R. David Murray added the comment:

I don't think that helps.  The issue here is that *sequences* are iterated over 
by incrementing an integer index.  If you change the size of the list, you are 
potentially changing which value any given index points to.  Presumably the 
tutorial writer thought this was intuitive, and indeed after years of Python 
programming I find it so.  I can see how a beginner might not, though :)

What if we replaced:

  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:

With:

  Sequence iteration is preformed by incrementing an implicit integer index 
until there are no more items in the sequence.  The sequence is *not* copied 
before iteration, so if you modify the sequence during iteration the value that 
is affected by the next iteration of the loop may not be the one you are 
expecting.  You can avoid this problem by iterating over a copy of the 
sequence, and the slice notation makes this especially convenient:

However, this section has a deeper problem.  It is introducing the 'for' 
statement, but explains what the for statement does in terms of sequences, when 
in fact the for statement now operates on any iterable, not just sequences.  
(Many Python programmers probably do not remember the time before the iteration 
protocol was added to the language :)

Fixing that problem not only requires rewriting the section, but also figuring 
out the best place to introduce the concept of the iteration protocol (which 
*might* be in this section, but it's been so long since I've looked over the 
tutorial that I can't say).

--
nosy: +r.david.murray

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue30826] More details in reference 'Looping through a list in Python and modifying it'

2017-07-07 Thread Terry J. Reedy

Terry J. Reedy added the comment:

Are you looking for something like:

Let it = iter(words).  When next(it) returns 'defenestrate', insertion at the 
beginning moves the original 'defenestrate' over so that next(words) returns 
'defenestrate' again.
?

--
nosy: +terry.reedy

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue30826] More details in reference 'Looping through a list in Python and modifying it'

2017-07-03 Thread Anmol Gupta

Anmol Gupta added the comment:

And also a small explanation for why there would be an infinite loop without 
creating a copy.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue30826] More details in reference 'Looping through a list in Python and modifying it'

2017-07-02 Thread Raymond Hettinger

Raymond Hettinger added the comment:

The example would be more clear if we replaced the opaque idiom "words[:]" with 
the more explicit alternative "words.copy()".

--
assignee: docs@python -> rhettinger
nosy: +rhettinger

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue30826] More details in reference 'Looping through a list in Python and modifying it'

2017-07-02 Thread Anmol Gupta

Anmol Gupta added the comment:

Wrong documentaion section linked.

Correct seciton: Section 4.2 on 
https://docs.python.org/3/tutorial/controlflow.html

The last line needs more explanation.

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue30826] More details in reference 'Looping through a list in Python and modifying it'

2017-07-01 Thread Anmol Gupta

New submission from Anmol Gupta:

Documentation section: 
https://docs.python.org/3/reference/compound_stmts.html#for

The documentation does not explain at all why is there an infinite loop when 
not using a copy of the list.

It leaves the reader in a confused state.

Even there are questions concerning the same on stackoverlflow: 
https://stackoverflow.com/questions/44633798/loop-through-a-list-in-python-and-modify-it

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue30826] More details in reference 'Looping through a list in Python and modifying it'

2017-07-01 Thread Anmol Gupta

Changes by Anmol Gupta :


--
assignee: docs@python
components: Documentation
nosy: Anmol Gupta, docs@python
priority: normal
severity: normal
status: open
title: More details in reference 'Looping through a list in Python and 
modifying it'
type: enhancement
versions: Python 3.6

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com