docs@ InfiniteLoop in example on website
Following example in Section 4.2 would run into infinite loop. https://docs.python.org/2/tutorial/controlflow.html#for-statements
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']
Should be something like this (but still leaves duplicate elements in the list): ----------------------------------- words_tmp = copy.deepcopy(words) for w in words_tmp: if len(w)>6 : words.insert(0,w) ----------------------------------- Thanks & Regards Abhinav Tyagi
Sorry for the inconvinience. I realised that the example is correct. Thanks & Regards Abhinav Tyagi On 12 April 2015 at 18:38, Abhinav Tyagi <abhityagi85@gmail.com> wrote:
Following example in Section 4.2 would run into infinite loop.
https://docs.python.org/2/tutorial/controlflow.html#for-statements
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']
Should be something like this (but still leaves duplicate elements in the list): ----------------------------------- words_tmp = copy.deepcopy(words) for w in words_tmp: if len(w)>6 : words.insert(0,w) -----------------------------------
Thanks & Regards Abhinav Tyagi
On 12/04/2015 14:08, Abhinav Tyagi wrote:
Following example in Section 4.2 would run into infinite loop.
https://docs.python.org/2/tutorial/controlflow.html#for-statements
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']
Except that it doesn't. You seem to have missed the entire point of that particular example, which is that [:] makes a copy of the list, so mutating the original list will not affect the loop. Did you actually try it in an interpreter? TJG
Just after I wrote the email, I realised I made a mistake and within few minutes I replied to my email that example was fine. On 12-Apr-2015 6:48 pm, "Tim Golden" <mail@timgolden.me.uk> wrote:
On 12/04/2015 14:08, Abhinav Tyagi wrote:
Following example in Section 4.2 would run into infinite loop.
https://docs.python.org/2/tutorial/controlflow.html#for-statements
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']
Except that it doesn't. You seem to have missed the entire point of that particular example, which is that [:] makes a copy of the list, so mutating the original list will not affect the loop.
Did you actually try it in an interpreter?
TJG
participants (2)
-
Abhinav Tyagi
-
Tim Golden