List of fields

1316
7
05-15-2014 06:33 AM
GasperZupancic
New Contributor
Hallo!

I need to check if a feature class includes a field called 'unwanted_field' and then delete it. I've been trying to accomplish that with an if statement on a field list gained by the arcpy.ListFields tool, but with no success. If I type in my 10.2.1 ArcMap Python window next code:

arcpy.ListFields('my_feature_class')

I get the following result:

[<Field object at 0x57483b0[0x623f860]>, <Field object at 0x5748290[0x623ff20]>, <Field object at 0x57485b0[0x623fd10]>, <Field object at 0x114465d0[0x623ff80]>, <Field object at 0x5742ff0[0x623fda0]>, <Field object at 0x587d570[0x623f650]>, <Field object at 0x11446670[0x623fa10]>, <Field object at 0x11446630[0x623fea8]>]

even though  my_feature_class includes 8 fields named: OBJECTID, SHAPE, Shape_Length, etc.

What am I doing wrong?

Thanks in advance,
Gašper
Tags (2)
0 Kudos
7 Replies
JamesCrandall
MVP Frequent Contributor
for fld in arcpy.ListFields('my_feature_class'):
  if fld.name == 'unwanted_field':
     'add field delete code here
   
0 Kudos
IanMurray
Frequent Contributor
You are doing nothing wrong, it just is printing the location on the harddrive of each field, instead of the name.  If you need to retrieve the actual name of the field, you need to use the .baseName or .aliasName method on each field.

More on that can be found getting field attributes here: http://resources.arcgis.com/en/help/main/10.2/index.html#//018z0000004n000000

If you are writing in the interative python window, you'd probably want something like this if you wanted to just delete the "unwanted_field".

fieldlist = arcpy.ListFields('my_feature_class')
 
for field in fieldlist:
  if field.baseName == "unwanted_field":
    arcpy.DeleteField_management('my_feature_class", field)


Hope this helps

Edit: Oops looks like James beat me to it.
0 Kudos
Zeke
by
Regular Contributor III
Try something like:

# 1. list fields (from here)
import arcpy  
featureclass = "c:/data/municipal.gdb/hospitals" 
field_names = [f.name for f in arcpy.ListFields(featureclass)] 

# 2. another method
import arcpy
lstflds = arcpy.ListFields("yourfeatureclass")
for fld in lstflds:
    if fld.name == "somefield":
        arcpy.DeleteField_management("yourfeatureclass", "somefield")

# 3. simpler, if you know your feature class; no need to check field name, you provide it
arcpy.DeleteField_management("yourfeatureclass", "somefield")


        
0 Kudos
AdamCox1
Occasional Contributor II
Hi Gaspar, what you're getting is a list of field objects, which, when printed out, looks like gibberish.  As jamesfreddyc wrote above, you need to access the name property of each field object.  You can condense this by using list comprehension:
if "fieldname" in [f.name for f in arcpy.ListFields(featureclass)]:
    #do something


Lists and dictionaries are extremely useful, so it's good to get familiar with them https://docs.python.org/2/tutorial/datastructures.html
0 Kudos
IanMurray
Frequent Contributor
I think we had an overkill of responses for this thread.
0 Kudos
AdamCox1
Occasional Contributor II
I think we had an overkill of responses for this thread.

Yeah, I think they all happened simultaneously too.
0 Kudos
GasperZupancic
New Contributor
Tkank you all!
0 Kudos