Fastest way to get number of rows in a da.SearchCursor

6593
5
Jump to solution
10-23-2014 08:31 AM
JohnDye
Occasional Contributor III

Anyone know of a quick way to count the number of records returned by a da.SearchCursor with a where_clause applied?

with arcpy.da.SearchCursor(MyLayer, "SHAPE@", SuperDuperAwesomeQuery) as rows:

    # how do I figured out how many records are in rows without iterating through rows???

Tags (4)
0 Kudos
1 Solution
5 Replies
JohnDye
Occasional Contributor III

Thanks James,

I was trying to avoid making a query table but it seems that's the the quickest approach that doesn't require iterating. However using the MakeQueryTable tool did require spinning up the geoprocessor which took some time so in the end I actually found it was still faster to just iterate and count the rows.

with arcpy.da.SearchCursor(FeatureLayer, "SHAPE@", SuperDuperAwesomeQuery) as rows:

    rowCount = 0

    for row in rows:

        rowCount = rowCount + 1

    print rowCount

    del rows

0 Kudos
JamesCrandall
MVP Frequent Contributor

Alternative method (I don't know it this is faster):

sql = "ObjectId=1"

_dfsource = r'H:\Documents\ArcGIS\Default.gdb\L8_Parcel'

_numparr = arcpy.da.FeatureClassToNumPyArray(_dfsource, ["ObjectId"], sql)

rowcount = len(_numparr)

print rowcount

ChrisPedrezuela
Occasional Contributor III

row count = len(list(i for i in arcpy.da.SearchCursor(fc, fields, query)))

JohnDye
Occasional Contributor III

Niiice. So obvious once I saw it. Sorry, I already gave the answer to James.

0 Kudos