Select to view content in your preferred language

direction of arcpy.da. Search and UpdateCursors

1082
2
Jump to solution
08-04-2020 01:08 PM
DavidPike
MVP Notable Contributor

If I'm using an arcpy.da.UpdateCursor to search for an ObjectID and delete the entry/row, is it possible to walk the cursor backwards for sake of speed?

Knowing that my feature has recently been created and would be near the last index, would the cursor be faster with a for loop going backwards (if such a thing) and a break statement after the deletion?

Or is the cursor set up more like a set, and unordered by OID?

Cheers

0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Alum

As far as I know, there is no way tot walk the cursor backwards. But you can include a where clause in the cursors

with arcpy.da.UpdateCursor(your_table, "OBJECTID", "OBJECTID=12345") as cursor:
    for row in cursor:
        cursor.deleteRow()

I did a little test on an Enterprise geodatabase (albeit with a SearchCursor).

The execution time of the loop solution predictably depended on the position of the row in the table. This solution did not and also was a little quicker than the loop. Your mileage may vary, of course.


Have a great day!
Johannes

View solution in original post

2 Replies
JohannesLindner
MVP Alum

As far as I know, there is no way tot walk the cursor backwards. But you can include a where clause in the cursors

with arcpy.da.UpdateCursor(your_table, "OBJECTID", "OBJECTID=12345") as cursor:
    for row in cursor:
        cursor.deleteRow()

I did a little test on an Enterprise geodatabase (albeit with a SearchCursor).

The execution time of the loop solution predictably depended on the position of the row in the table. This solution did not and also was a little quicker than the loop. Your mileage may vary, of course.


Have a great day!
Johannes
DavidPike
MVP Notable Contributor

Of course!

This will be optimal, thanks.

0 Kudos