24

I have a question regarding selections in ArcGIS for Desktop. Assumed I have one layer in ArcMap and I have selected two of five features.

Is it possible to get a list of all selected features by using Python?

It would be fine if there is a way to get one special (or all) attribute(s) of the selected features stored in a list that can be written into a txt file.

Is it possible to do this in ArcGIS for Desktop?

2 Answers 2

36

Any time you have a selection on a layer a cursor object will only return the selected rows.

for row in arcpy.SearchCursor("name_of_layer_with_selection"):
    print row.field1, row.field2
2
  • 7
    But the problem though is that if you get all the features returned you don't know if ALL or NONE were selected.
    – Matej
    Commented Apr 21, 2014 at 14:01
  • 2
    Does this also apply to selected features in feature class ?
    – Jio
    Commented Sep 4, 2015 at 18:22
15

the Describe function will also return a list. I am not sure if this is faster than the cursor method but I have fond this to be a useful tool. The resulting list is the object id's for the selection set.

import arcpy

aa = arcpy.Describe("someFC")
ss = aa.FIDset
tt = ss.split("; ")
Print tt

[u'1363', u'1364', u'1365', u'1367', u'1369', u'1370']
4
  • Good solution too! Sadly I am not able to set two times the green heel. This solution makes the script also independent from different ArcGIS Versions, because in ArcGIS 10.1 the cursors are called in a different way than in ArcGIs 10.0 (ArcGIS 10.1 arcpy.da.SearchCursor, ArcGIS 10.0 arcpy.SearchCursor...).
    – Sven
    Commented Dec 31, 2012 at 20:38
  • 4
    Both cursor types are available at 10.1. You don't have to use the new arcpy.da cursors.
    – blah238
    Commented Dec 31, 2012 at 20:53
  • 1
    This answer gives a way to check for empty selection, which would prevent inadvertently calling a tool on an entire feature class when in fact it was zero features that happened to fulfill your selection criteria.
    – nickbrick
    Commented Oct 18, 2017 at 16:28
  • Great tip @Sam Flarity, this is a nice (faster?) alternative to using arcpy.SearchCursor or arcpy.da.SearchCursor
    – grego
    Commented Jun 14, 2018 at 0:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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