Python Forum

Full Version: Understanding slice copying in for loop
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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."

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.
Hello and welcome!

It's called slicing.
You can slice a sequence in python like this: words[1:4].
That returns all elements from index 1 to index 4. The index 4 is not included.
If you want all elements from index 1 to the end you are doing it like this: words[1:].
Respectively, from the beginning to some index: words[:4].
As you already have guessed the [:] notation is representing the whole sequence. From the beginning to the end. In fact a copy of ( in this case ) the list.
I think the more important is the reason why the code iterate over a copy of the list.
In the loop you add the words with len > 6 to the list. You should not change the list you iterate over. In this particular case you will enter a infinite loop. If you delete elements from a list you iterate over - you will get unexpected results as some elements will never be used in the loop.
(Jul-02-2018, 07:31 AM)wavic Wrote: [ -> ]Hello and welcome!

It's called slicing.
You can slice a sequence in python like this: words[1:4].
That returns all elements from index 1 to index 4. The index 4 is not included.
If you want all elements from index 1 to the end you are doing it like this: words[1:].
Respectively, from the beginning to some index: words[:4].
As you already have guessed the [:] notation is representing the whole sequence. From the beginning to the end. In fact a copy of ( in this case ) the list.

Does that mean there are two lists with same name words?
(Jul-02-2018, 09:36 AM)yksingh1097 Wrote: [ -> ]Does that mean there are two lists with same name words?
No, the copy of the list is not assigned to any variable name. It is used in the loop and then garbage collected.
Or if you prefer, one name, in this case words always points to one object
(Jul-02-2018, 09:50 AM)buran Wrote: [ -> ]
(Jul-02-2018, 09:36 AM)yksingh1097 Wrote: [ -> ]Does that mean there are two lists with same name words?
No, the copy of the list is not assigned to any variable name. It is used in the loop and then garbage collected.
Or if you prefer, one name, in this case words always points to one object

Okay now I understand,Thanks Buran.