Let's say I have a layer made up of pre-selected features from another layer:

selects = arcpy.SelectLayerByAttribute_management (S_FIRM_Pan, "NEW_SELECTION", "DFIRM_ID = '{}'".format(fipC))

If I get a count of the number of these selected features, I get says, 199.

getCount = arcpy.GetCount_management(selects)

Checking in arcMap, I can see that this is the correct number. I then try to use arcpy.da.SearchCursor to get all of the values for a certain field for selects:

raw_firm_pan = [row[0] for row in arcpy.da.SearchCursor(selects, "FIRM_PAN")]

If I use print(len(raw_firm_pan)) it only gives me 198 items. When I print it out and compare it to the actual rows for selects in arcMap, it appears that the last row is being cut off! How can I fix this?

  • 2
    selects is a result object, not a layer which you can pass to the SearchCursor. Try: raw_firm_pan = [row[0] for row in arcpy.da.SearchCursor(S_FIRM_Pan, "FIRM_PAN") then len(raw_firm_pan) Or try passing the layer to getCount: arcpy.GetCount_management(S_FIRM_Pan) – BERA Sep 20 '18 at 18:22
  • The problem with this approach is I don't want to search all of the layer S_FIRM_Pan, only the items "selected" by attribute; hence my selects. And despite it being a results object, I am able to get all of the items from selects into list raw_firm_pan. All of them except the last row. It is the missing values from the last row that is the mystery. – gwydion93 Sep 20 '18 at 18:37
  • 2
    After you use select by attribute S_FIRM_Pan will only consist of the selected features. The "unselected" S_FIRM_Pan will have been replaced – BERA Sep 20 '18 at 18:41
  • 3
    If you want to keep S_FIRM_Pan use MakeFeatureLayer on it and pass the sql statement to this. You will then get a new layer with only the matching records and not mess up your original layer. – BERA Sep 20 '18 at 18:48
  • arrays start at 0..... not sure if this applies to your count issue. – NULL.Dude Sep 20 '18 at 20:02

I would use this coding pattern instead:

raw_firm_pan = [row[0] for row in arcpy.da.SearchCursor(S_FIRM_Pan, "FIRM_PAN", "DFIRM_ID = '{}'".format(fipC))]

I have applied the selection using a where_clause when creating the cursor object instead of using the arcpy.SelectLayerByAttribute_management() function. I rarely use the latter because many ArcPy functions allow you to use a where_clause.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.