[GIS] Stop a Cursor loop when only 1 item is selected with Python

arcpycursorloop

I am trying to create a Cursor that goes through a selection process and stops looping when only one feature is selected. Basically, as the Cursor loops it subsets a selection, and instead of going through every row of the table I'd like to save time and stop the Cursor when one item is selected. So it would look something like this:

for row in rows:
    ... (selecting process) ...
    if [number of features selected] = 1:
        [stop cursor]
    else:
        rows.updateRow(row)
del row, rows

Any advice? I can't seem to even find any way to stop a cursor other than deleting it but I don't think I could do that type of command from within the cursor itself.

Best Answer

I usually do this using arcpy.GetCount_management()

If you set it up like this:

if int(arcpy.GetCount_management(lyr_Name).getOutput(0)) == 1:
    break
else:
    [whatever else you were going to do]

It should kick your code out of the loop when the number of selected features in lyr_Name is one.

Here's the help page from ESRI for the function.

Related Question