How to determine number of records using arcpy.da.SearchCursor

42300
13
Jump to solution
03-21-2014 10:46 AM
BruceBacia
Occasional Contributor
Does anyone know of a quick way to return the number of records in a query constrained Search Cursor using the da module?

rows = arcpy.da.SearchCursor(someFC,someFields,someQuery)  print (len(rows))  #I'm sure this used to work with the old cursors, but doesn't work with da.SearchCursor
Tags (2)
1 Solution

Accepted Solutions
MattEiben
Occasional Contributor
You were actually very close in your original entry.  Try this:

rows = [row for row in arcpy.da.SearchCursor(someFC,someFields,someQuery)]  print len(rows)


The cursor creates an iterator, which you can't find the length of.  The key is to use list comprehension for a nice one liner with the cursor to make your list.

Of course, you can always just this as an alternative:

int(arcpy.GetCount_management(someFC).getOutput(0))


Hope this helps!

View solution in original post

0 Kudos
13 Replies
JamesCrandall
MVP Frequent Contributor
This works (and should honor any selections):


myFeatClass = r'C:\MyGDB\myFeatureClass'
result = int(arcpy.GetCount_management(fc).getOutput(0))

print result



This also works:


rowcount = 0
myFeatClass = r'C:\MyGDB\myFeatureClass'
with arcpy.da.SearchCursor(myFeatClass, "*") as cursor:
    for row in cursor:
        count = count + 1

print rowcount



Edit:  You can also apply an sql conditional statement too


rowcount = 0
sql = "OBJECTID = 29821"
myFeatClass = r'C:\MyGDB\myFeatureClass'
with arcpy.da.SearchCursor(myFeatClass, "*", sql) as cursor:
    for row in cursor:
        count = count + 1

print rowcount

BruceBacia
Occasional Contributor
Thanks for the suggestions, but I'm looking to avoid a counter.
0 Kudos
JamesCrandall
MVP Frequent Contributor
Thanks for the suggestions, but I'm looking to avoid a counter.


arcpy.GetCount_management doesn't work for you?
0 Kudos
MattEiben
Occasional Contributor
You were actually very close in your original entry.  Try this:

rows = [row for row in arcpy.da.SearchCursor(someFC,someFields,someQuery)]  print len(rows)


The cursor creates an iterator, which you can't find the length of.  The key is to use list comprehension for a nice one liner with the cursor to make your list.

Of course, you can always just this as an alternative:

int(arcpy.GetCount_management(someFC).getOutput(0))


Hope this helps!
0 Kudos
JasonScheirer
Occasional Contributor III
Please, please, please use James' GetCount solution:

result = int(arcpy.GetCount_management(fc).getOutput(0))


It will be the fastest.
FilipKrál
Occasional Contributor III
Indeed, use arcpy.GetCount_management. In order to limit the number of record by a where clause, create a feature layer (or table view) first.

fc = r'c:\path\to\fc'
w = '"OBJECTID" < 10'
lr = arcpy.MakeFeatureLayer_management(fc, 'tmp_layer', w).getOutput(0)
result = int(arcpy.GetCount_management(lr).getOutput(0))
arcpy.Delete_management(lr) # deletes the layer, not the feature class data
# result is 9
benberman
Occasional Contributor
I actually just built one like this today.

lst = [row.getvalue() for row in arcpy.SearchCursor(dataset,expression...enter other parameters you want to include)]
print len(lst)
0 Kudos
benberman
Occasional Contributor
I would err on the side of the SearchCursor rather than GetCount in case if a request may arise when he may need to include queries on the dataset.
0 Kudos
ChrisSnyder
Regular Contributor III
The number of records determines what method (GetCount or da.SearchCursor) is faster.

If the number of records is quite large, GetCount is faster, otherwise the searchcursor one liner is faster. I use the latter method (cursor) in some code that recursively (and quickly) needs to determine selected set counts.

Jason - Curious why you indicate the cursor method is a poor choice (locks?, memory?, ???).

import arcpy, time
fc = r"C:\csny490\overlay_20130620\ldo_20130620\ldo_database.gdb\crew_code"
time1 = time.clock()
result = int(arcpy.GetCount_management(fc).getOutput(0))
time2 = time.clock()
print "GetCount (" + str(result) + "), took " + str(time2-time1) + " seconds..."
time1 = time.clock()
result = len([r[0] for r in arcpy.da.SearchCursor(fc, ["OID@"])])
time2 = time.clock()
print "SearchCursor (" + str(result) + "), took " + str(time2-time1) + " seconds..." 


Some results:

Table with 8 records:
GetCount (8), took 0.755854384327 seconds...
SearchCursor (8), took 0.136624540949 seconds...

Table with 81k records:
GetCount (81327), took 0.996590826688 seconds...
SearchCursor (81327), took 0.648470391747 seconds...

Table with 2 mm records:
GetCount (2133404), took 4.37459715564 seconds...
SearchCursor (2133404), took 11.5886194669 seconds...