ArcPy Field Name List

Frequently, especially when using cursors, I need to quickly get a list of field names not including the ObjectID nor Geometry fields. I accomplish this by using a quick shorthand function.

fields_list = lambda feature_class: [field.name for field in arcpy.ListFields(feature_class) if field.type != 'Geometry' and field.type != 'OID']

True, it is not very Pythonic. There are a few things happening in there and the conditional statement likely could be condensed. It works for what I need, though.

Here is the breakdown of what is going on. It is mostly a list comprehension, a shorthand for building Python Lists. This list comprehension is wrapped into a lambda function, another shorthand, this one for creating functions on the fly. If exploding this out into a longer form, it looks like this.

def fields_list(feature_class):

    # variable to store list
    fields = []

    # for every field in the feature class
    for field in arcpy.ListFields(feature_class):

        # if the field is not geometry nor oid
        if field.type != 'Geometry' and field.type != 'OID':

            # append the field name to the list
            fields.append(field.name)

    # return the list of field names
    return fields

While easier to read, the former is easier to cut and paste. This is why I use it more frequently. Hopefully this makes your life easier when data munging, something I frequently find myself doing.