This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: For loop exit early
Type: Stage:
Components: Library (Lib) Versions:
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: mike_smith, rhettinger, tim.peters
Priority: normal Keywords:

Created on 2006-02-11 00:13 by mike_smith, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (3)
msg27482 - (view) Author: msmith (mike_smith) Date: 2006-02-11 00:13
When I run the following snippet the "for" loop exits
early, not examining every item in the "lines" list. 
It will only print part of the list, i.e., only
approximately 65% of any list I use is printed.  (E.g.,
a list of 100 items only about 65 is printed)

If I wrap the for statement in another for statement
with a range() operator it works.

I'm pretty new to scripting, so I'm sure there's a
better way to do what I'm trying; but this seems like a
 bug.

Thanks for your help,

=========================

count = 0
lines =
["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16"]
def getnextline():
		l = lines.pop()
		l = l.strip()
		return l
for x in lines:
	count += 1
	newline = "%s,%s,\n" % (getnextline(),getnextline())
	print count, ":", newline	

Output:
---------
1 : 16,15,

2 : 14,13,

3 : 12,11,

4 : 10,9,

5 : 8,7,

6 : 6,5,
msg27483 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2006-02-11 00:25
Logged In: YES 
user_id=80475

Sorry, the bug is in your code (the for-loop is looping 
over a list that is being mutated by pops).  I recommend 
posting on comp.lang.python or to the python-tutor list to 
get feedback on how to write this correctly.
msg27484 - (view) Author: Tim Peters (tim.peters) * (Python committer) Date: 2006-02-11 04:34
Logged In: YES 
user_id=31435

Changed "group" to not-a-bug.
History
Date User Action Args
2022-04-11 14:56:15adminsetgithub: 42885
2006-02-11 00:13:22mike_smithcreate