G+_Justin Eiesland Posted April 23, 2014 Share Posted April 23, 2014 I am new to programming and was wondering what was wrong with the companions.pop() line in the Episode 12 example where it would not return all three items in the list. Link to comment Share on other sites More sharing options...
G+_Dan Sarauer (N3m1sys) Posted April 23, 2014 Share Posted April 23, 2014 See Lee's errata post from that episode (#3) explains it quite nicely. http://goo.gl/ESRIKh Link to comment Share on other sites More sharing options...
G+_Justin Eiesland Posted April 23, 2014 Author Share Posted April 23, 2014 Thanks! Link to comment Share on other sites More sharing options...
G+_Joe Maruschek Posted April 24, 2014 Share Posted April 24, 2014 To further expand on what Lee Crocker said, it appears that Python internally is keeping a counter going when you loop over a list. So the first time through, it picks the first item in the list, the second time it gets the 2nd item, the 3rd time through selects the 3rd item, and so on. In fact, it gets the current 2nd or 3rd item, even if the list has changed. So, the first time time through, the companions list has three items, and then we do a pop(). Now the 2nd time through the loop, there are only two items in the list. If they would have printed out the current item in the for loop, you would have seen that it was the current 2nd item, which is now the last item in the list. So then the pop() was done the 2nd time, now there is only one item left in the list. The 3rd time through the loop Python is actually looking for the 3rd item, but there is none. That's why it stops. Link to comment Share on other sites More sharing options...
Recommended Posts