I am just learning Python and in the documentation the below example is mentioned and they have not mentioned the reason for this behavior.
words = ['cat', 'window', 'defenestrate']
for w in words[:]:
if(len(w) > 6):
words.insert(0,w)
From what I understand the above would iterate over the elements by first creating a copy of the original array and the syntax [:] means from the start position to the end position of the copy.
The documentation mentions that if instead of the above syntax we used the syntax
for w in words:
if(len(w) > 6):
words.insert(0,w)
It would create an infinite list with the word 'defenestrate' being inserted over and over again.
Can someone please explain such behavior and also if I could find the answer to my question by referring some other documentation.
words.insert(0,w)