Python Help

2344
6
Jump to solution
12-10-2014 08:22 AM
DavidLeslie
New Contributor II

Is there a way I can create a list of only the deletable fields in a feature class (and by deletable I mean fields other than OBJECTID, Shape, etc . I tried using arcpy.ListFields(), but it list all of the feilds.

Tags (2)
1 Solution

Accepted Solutions
DavidBlanchard
Esri Contributor

You will have to filter your list of fields produced by arcpy.ListFields() using the properties of the Field object. If you need help on filtering your list, the Dive Into Python guide has an excellent chapter on this: 4.5. Filtering Lists.

View solution in original post

6 Replies
DavidBlanchard
Esri Contributor

You will have to filter your list of fields produced by arcpy.ListFields() using the properties of the Field object. If you need help on filtering your list, the Dive Into Python guide has an excellent chapter on this: 4.5. Filtering Lists.

DavidLeslie
New Contributor II

Thanks for your quick reply! From your post, I realized that I could use the editable method to check to see if a field can be deleted or not!

0 Kudos
DanPatterson_Retired
MVP Emeritus

just be aware of the subtle difference between editable and required

...

Editable fields:

[[False, u'FID'], [True, u'Shape'], [True, u'Id'], [True, u'X'], [True, u'Y'], [True, u'Label']]

Required fields:

[[True, u'FID'], [True, u'Shape'], [False, u'Id'], [False, u'X'], [False, u'Y'], [False, u'Label']]

...

0 Kudos
DanPatterson_Retired
MVP Emeritus

like this...

>>> import arcpy
>>> shp_file = "c:/temp/x.shp"
>>> flds = arcpy.ListFields(shp_file)
>>>
>>> req_flds = [ [fld.required, fld.name] for fld in flds]
>>> req_flds
[[True, u'FID'], [True, u'Shape'], [False, u'Id'], [False, u'X'], [False, u'Y'], [False, u'Label']]
>>>
0 Kudos
DavidLeslie
New Contributor II

I just used:

for i in range(len(parcelFieldList)):
     if parcelFieldList.editable:
          parcelFieldNames.append(str(parcelFieldList.name))
0 Kudos
curtvprice
MVP Esteemed Contributor

List comprehensions are just so elegant.

efields = [f.name for f in arcpy.ListFields(table) if f.editable]

0 Kudos