SearchCursors and For Loops - why do they only work once?

Discussion created by j256 on Jan 18, 2012
Latest reply on Jan 19, 2012 by lpinner
I have created a very simple example to explain my problem. I have a dbf file that looks like this, with just two fields:

Title    Name
Mr      Mike
Miss    Jones
Dr      Smith
Mr      Parker

The following code...

table = r"C:\Names.dbf"
tablerows = arcpy.SearchCursor(table)
for item in tablerows:
  print item.getValue("Title")

...produces this result in the output window (as expected):





Now, here's the weird bit. Since I've already declared tablerows, I want to loop through that list again. So, I enter the following code:

for item in tablerows:
  print item.getValue("Title")

...but bizarrely nothing happens because somehow ArcGIS has 'forgotten' what tablerows, or, even more strangely, the act of looping through it has somehow 'used up' the variable, and it has to be recharged by declaring the searchcursor again. Can anyone explain this utterly bizarre behaviour please?