1

I use arcpy.ListFields() method for various purposes and usually the only fields that I need are the ones added by the users. ListFields lists all fields including the ones comes in by default with any feature classes (e.g., OBJECTID, FID, Shape, SHAPE_Area, etc.), and I was wondering if there is an elegant way of omitting these fields from listing.

I can do, for example for GDB feature classes,

[f.name for f in arcpy.ListFields("sample_fc") if f.type not in ["OID","Geometry"] or f.name.upper() not in ["SHAPE_AREA", "SHAPE_Length"]]

but this does not save me from getting "SHAPE_Area1" or other variants of inherited fields in the list.

2

You can use the required property of the Field object to make the difference between fields managed by ArcGIS (OID en shape fields), and fields added by the user:

[f.name for f in arcpy.ListFields("sample_fc") if not f.required]

But if you (your geoprocessing workflow) make a copy of required fields (like 'SHAPE_Area1' seems to be), these copied fields won't be excluded from the list comprehension above (since they were added by the user).

What I use to do when my workflow adds inherited OID and shape fields from source datasets, is listing the fields I do need and deleting all the rest (except the required fields of course):

keep_fields = ["abc", "def"]
for f in arcpy.ListFields("sample_fc"):
  if not f.required:
    if not f.name in keep_fields:
      arcpy.DeleteField_management("sample_fc", f.name)
| 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.