I couldn't find an explanation for the code in python doc article 4.2 regarding the for loop.
It mentioned something like: if we don't make a copy of the list, then it will print infinite list values for the list words = ['cat', 'window', 'defenestrate']
; but not if we make a copy of it beforehand using "for w in words[:]"
. I need an explanation for this.
words = ['cat', 'window', 'defenestrate']
for w in words :
if len(w) > 6:
words.insert(0,w)
This code will run in an infinite loop, but not if we swap the for w in
wordswith
for w in words[:]`