2

I'm currently learning Python, and I struggle to understand what the slicing [:] does in the for loop shown below.
I failed to find any questions about the [:] syntax, so I am posting a question.

I understand that writing namesList[2:] would slice the list from index 2: ['three', 'four', 'five']
And namesList[:2] would slice the list until index 1: ['one', 'two'].

  1. If in the for loop I only put for nm in namesList: it is stuck on an infinite loop, constantly inserting 'four' to index 0. Why is it stuck?
  2. From what I can see in debugging, in the for loop with namesList[:], it continues from where it stopped. But why namesList: doesn't continue, and namesList[:] does? How does [:] work, what does the syntax mean?

names = "one two three four five"
namesList = names.split()    
for nm in namesList[:]:
    if nm[0] == "f":
        namesList.insert(0, nm)
print(namesList)

# Output:
['five', 'four', 'one', 'two', 'three', 'four', 'five']

names = "one two three four five"
namesList = names.split()    
for nm in namesList:
    if nm[0] == "f":
        namesList.insert(0, nm)
print(namesList)

# Infinite loop
Kronecker
  • 21
  • 4
  • 4
    Does this answer your question? [Understanding slice notation](https://stackoverflow.com/questions/509211/understanding-slice-notation) – Brian61354270 Apr 25 '21 at 15:33
  • 2
    Does https://stackoverflow.com/questions/4081561/what-is-the-difference-between-list-and-list-in-python or https://stackoverflow.com/questions/44633798/what-is-the-meaning-of-list-in-this-code help? – Bill Lynch Apr 25 '21 at 15:33
  • @BillLynch, Thank you the first link helped me understand https://stackoverflow.com/questions/4081561/what-is-the-difference-between-list-and-list-in-python – Kronecker Apr 25 '21 at 15:39

1 Answers1

2

It's a nice (if a bit arbitrary) example why you should not modify a list (or other data structure) while iterating over it.

In your second code, your loop eventually arrives at the four, and thus decides to insert something at the beginning of the list.

But that shifts all other elements in the list one forward, which the loop doesn't know, so now it sees the four again. And again, and again...

What the slice does is essentially create a copy of the list. That way the loop only ever runs over the original parts of your list and thus modifying that list is save.

The reason it copies your list is that in general my_list[3:5] or so would pick the elements starting at the first index and running to (but excluding) the last index. If you omit the first index, it defaults to 0, and if you omit the last index it defaults to the last element, so [:] just means "pick all elements, from 0 to last".

cadolphs
  • 9,014
  • 1
  • 24
  • 41
  • Thank you for explaining in detail why [:] copies the original list. I agree you shouldn't modify a list while iterating over it, it is just the example the lecture I am learning from uses. – Kronecker Apr 25 '21 at 15:45
  • 1
    Aside from answering a very common duplicate, your answer only tangentially explains why this happens. **List slices return copies by definition.** This is not trivial and not all slices in python create copies. – Andras Deak -- Слава Україні Apr 25 '21 at 15:45