I just went through the Looping Techniques Chapter of the Python docs tutorial and I have a question regarding this boy here: [:]
I learned that it takes the start and end index of a string, so:
text = "This is text"
text[:] # will return the whole text "This is text" and
tex[:4] # will return "This".
But when I saw this code here...
words = ['cat', 'dog', 'cowcowcow']
for w in words[:]: # Loop over a slice copy of the entire list.
if len(w) > 6:
words.insert(0, w)
print words
Output:
['cowcowcow', 'cat', 'dog', 'cowcowcow']
...I didn't understand the meaning of [:]
in the for loop. I would just write
for w in words:
but when I do so it's an endless while loop, why?