Python Forum
Understanding slice copying in for loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Understanding slice copying in for loop
#1
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.
Reply
#2
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.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
(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?
Reply
#5
(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
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Fix pandas copy/slice warning. deanhystad 3 758 Sep-07-2023, 03:18 PM
Last Post: deanhystad
  Slice creates new objects? fmr300 4 1,268 Jul-20-2022, 12:34 PM
Last Post: fmr300
  InvalidIndexError: (slice(None, None, None), slice(None, -1, None)) SuperNinja3I3 1 4,288 Jul-15-2022, 05:59 AM
Last Post: Larz60+
  Slice list Moris526 1 1,619 Dec-24-2020, 02:19 AM
Last Post: deanhystad
  increase and decrease a slice value? KEYS 2 2,055 Nov-10-2020, 11:35 PM
Last Post: KEYS
  understanding basic loop behaviour vinci 5 2,817 Feb-11-2020, 09:53 PM
Last Post: vinci
  Pass Tuple as a Slice nagymusic 2 2,320 Dec-12-2019, 04:42 AM
Last Post: nagymusic
  Preferred way to slice a list SvetlanaofVodianova 3 2,485 Dec-09-2019, 11:50 PM
Last Post: SvetlanaofVodianova
  slice python array on condition Gigux 2 2,230 Nov-03-2019, 07:21 PM
Last Post: Larz60+
  How to append and drop to next line while slice/indexing emryscass 3 2,554 Sep-26-2019, 01:06 PM
Last Post: Malt

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020