Jul-02-2018, 07:15 AM
I was studying for loops ,there it was said that
"If you need to modify the sequence you are iterating over while inside the loop (for example to duplicate selected items), it is recommended that you first make a copy. Iterating over a sequence does not implicitly make a copy."
"If you need to modify the sequence you are iterating over while inside the loop (for example to duplicate selected items), it is recommended that you first make a copy. Iterating over a sequence does not implicitly make a copy."
words = ['cat', 'window', 'defenestrate'] for w in words[:]: # Loop over a slice copy of the entire list. ... if len(w) > 6: ... words.insert(0, w) ...
Output:>>> words
['defenestrate', 'cat', 'window', 'defenestrate']
According to me words[:] in for loop is a copy of words list. Please correct me if i am wrong and i also want to know the internal structure of words[:] in for loop, i.e., how it is working internally(how it made the copy). Thanks in advance.