0

Dear StackOverFlow, I just starting learning python, and I'm a little confused when it comes to looping through lists. I have been using Python.org's official python tutorial to learn. In section 4.2 on this page https://docs.python.org/2/tutorial/controlflow.html, in the last example they write this code:

for w in words[:]:
    if len(w) > 6:
        words.insert(0, w)

The part that confuses me is for w in words[:]: Maybe I need to read their description a little better, but I can't seem to figure out why the previous code works and this doesn't:for w in words:. I guess it confuses me because when i type words[:] in the interpreter I get the same result when i type words in the interpreter. If you could shed some light on this topic, I would be very grateful.

yoprogrammer
  • 91
  • 1
  • 6
  • 2
    `words[:]` creates a *copy* of the list. It is a new, distinct object. It won't change when you insert into the original list. See [How to clone or copy a list in Python?](http://stackoverflow.com/q/2612802) – Martijn Pieters Aug 14 '15 at 00:37
  • I would clarify that its length and the addresses it points to won't change, but the overall contents may change if the copied `list` contains other mutable objects (like `list`s). – TigerhawkT3 Aug 14 '15 at 00:40

1 Answers1

0

In the for loop, you're inserting elements into the words list. If you iterate directly over the words list, the elements that you insert are going to affect the iteration. In general, when modifying a list that you're iterating over, you might get an incorrect result, an exception might be raised, or you could even have an infinite loop as the list grows until you run out of memory.

In your particular case, if there's any w in the list for which len(w) > 6, you're going to get an infinite loop. Say you have the list word = ['len(this) == 15']. In the first iteration, since len(w) > 6, it's going to insert w at position 0 of words, therefore pushing everything else in the list forward. So now words is equal to ['len(this) == 15', 'len(this) == 15']. Now the for loop checks to see if there's a next element; there's guaranteed to be, because you just inserted an element at position 0, so the for loop iterates again. Of course, w contains the same value it did in the last iteration, so the same thing happens. To infinity, and beyond!

Cyphase
  • 11,502
  • 2
  • 31
  • 32
  • 1
    Thanks alot! I think I understand it now. So I iterate through the copy of the list but modify the original list, so I don't affect the iteration process potentially creating an infinite loop or causing an exception. Thanks for the help kind sir. – yoprogrammer Aug 14 '15 at 03:18
  • @yoprogrammer, you're welcome. If you could upvote as well, I'd appreciate it :). It's [the SO way](https://stackoverflow.com/help/someone-answers). – Cyphase Aug 14 '15 at 03:21
  • definitely will upvote, but I seem to be having an issue doing so. As soon as I upvote your post it automatically downvotes it back to zero. Not sure why it's doing this... – yoprogrammer Aug 14 '15 at 03:27