AnsweredAssumed Answered

Enumeration of a cursor

Question asked by blake.terhune on Apr 9, 2015
Latest reply on Apr 13, 2015 by blake.terhune
Branched from an earlier discussion

Xander, I recently unlocked the mysteries of enumeration and noticed your cnt variable does something similar. Because I'm still learning, I thought something like this would work instead:

with arcpy.da.SearchCursor(fc_house, flds_house) as curs_in:
    for cnt, row_in in enumerate([i for i in curs_in], start=1):
        curs_in.reset()  ## Return cursor back to the first row after enumeration
        if cnt % 25 == 0:
            print "Processing connection: {0}".format(cnt)

        pnt1 = row_in[0]
        parcel_id = row_in[1]
        date_sale = row_in[2]
        fire_oid = row_in[3]
        # Continue processing rows in cursor...


After some tinkering, I realized that when you enumerate a cursor like this, it goes through all the rows. The problem is that the cursor object ends with no more rows and you have to call reset() on the cursor to start it back at the first row again. Since there is still more processing to be done, you'll essentially be iterating over all rows in the cursor twice with enumeration instead of once with your original counter variable. Do you think the extra time it takes to create the enumeration of the cursor is worth it in a case like this? Maybe only for cursors with a small number of rows?