[docs] docs@ InfiniteLoop in example on website

Tim Golden mail at timgolden.me.uk
Sun Apr 12 15:17:58 CEST 2015


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



More information about the docs mailing list