April 12, 2015
1:17 p.m.
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