[issue16225] list.remove in for loop

2012-10-14 Thread Ian Carr-de Avelon
New submission from Ian Carr-de Avelon: I'm new to Python and I've hit what appears to me to be a bug, but may be a feature, so a tutorial bug. I tried to loop through the items in a list, test each and remove those which fail the test. Simplifying to illustrate: print test [1, 2, 3, 4, 5]

[issue16225] list.remove in for loop

2012-10-14 Thread Chris Jerdonek
Chris Jerdonek added the comment: I have worked with languages where you are explicitly warned that you must not mess with the loop variable There is a warning in this part of the tutorial: It is not safe to modify the sequence being iterated over in the loop... (from

[issue16225] list.remove in for loop

2012-10-14 Thread Chris Jerdonek
Chris Jerdonek added the comment: Attached is a simple way of addressing this (essentially copying the verbiage and example from the other page). If we want, we could make the sample code different so that the reader doesn't see the same thing twice. -- keywords: +patch stage: -

[issue16225] list.remove in for loop

2012-10-14 Thread Serhiy Storchaka
Serhiy Storchaka added the comment: It is safe to modify a sequence during iteration if it's size not increased. words = ['cat', 'window', 'defenestrate'] for i, w in enumerate(words): ... if len(w) 6: ... words[i] = w[:5] + '…' ... words ['cat', 'window', 'defen…'] --

[issue16225] list.remove in for loop

2012-10-14 Thread Chris Jerdonek
Chris Jerdonek added the comment: It is safe to modify a sequence during iteration if it's size not increased. What do you mean by safe? The example given by the original commenter does not increase the size either. I believe safe is meant in the sense of avoiding possibly unexpected

[issue16225] list.remove in for loop

2012-10-14 Thread Georg Brandl
Georg Brandl added the comment: Well, I guess Serhiy meant neither increase nor decrease. In the end, the exact behavior will never be clear if you don't state explicitly how it's implemented: a counter that starts at 0 and is increased on __next__ until it's equal to len(list) (as checked at

[issue16225] list.remove in for loop

2012-10-14 Thread Chris Jerdonek
Chris Jerdonek added the comment: But in general the advice should be: if you want to insert or remove elements during iteration, iterate over a copy. I would expand this to cover changing the list in any way. I think the point being made is that iteration doesn't implicitly make a copy.

[issue16225] list.remove in for loop

2012-10-14 Thread Chris Jerdonek
Chris Jerdonek added the comment: Attaching revised patch. -- Added file: http://bugs.python.org/file27573/issue-16225-2-default.patch ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue16225 ___

[issue16225] list.remove in for loop

2012-10-14 Thread Chris Jerdonek
Chris Jerdonek added the comment: Reattaching. I duplicated a variable definition that was defined previously. -- Added file: http://bugs.python.org/file27575/issue-16225-3-default.patch ___ Python tracker rep...@bugs.python.org

[issue16225] list.remove in for loop

2012-10-14 Thread Serhiy Storchaka
Serhiy Storchaka added the comment: I mean it does not lead to crash, hang, etc. Even growing list during iteration can be safe you move forward faster than list grows or if you known where to stop. Unexpected behavior for one people can be expected for others. --

[issue16225] list.remove in for loop

2012-10-14 Thread Chris Jerdonek
Chris Jerdonek added the comment: I mean it does not lead to crash, hang, etc. I agree. I removed the word safe in the patch I attached to reduce ambiguity. Regarding unexpected behavior, remember that the tutorial is for beginners/newcomers. --

[issue16225] list.remove in for loop

2012-10-14 Thread Serhiy Storchaka
Serhiy Storchaka added the comment: I agree. I removed the word safe in the patch I attached to reduce ambiguity. Yes, so much the better. It will be nice somewhere in deep clarify for experts what happens with list iterator if the list changed. And iterating over modifyed (if you

[issue16225] list.remove in for loop

2012-10-14 Thread Chris Jerdonek
Chris Jerdonek added the comment: It will be nice somewhere in deep clarify for experts what happens with list iterator if the list changed. There is a note somewhat to this effect here: http://docs.python.org/dev/reference/compound_stmts.html#the-for-statement Note There is a subtlety

[issue16225] list.remove in for loop

2012-10-14 Thread Jesús Cea Avión
Changes by Jesús Cea Avión j...@jcea.es: -- nosy: +jcea ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue16225 ___ ___ Python-bugs-list mailing list