Using fields and indexes

When described, feature classes and tables have a fields property that returns a Python list of field objects, and an indexes property that returns a Python list of index objects. Each field or index object has a number of properties that can be used to explore the object. Alternatively, the ListFields and ListIndexes functions can be used to create the same lists. The following example shows how to create a field list and loop through the contents to find a specific field.

import arcpy

fc = "D:/St_Johns/data.gdb/roads"

# Describe a feature class
#
desc = arcpy.Describe(fc)

# Get a list of field objects from the describe object
#
fields = desc.fields

for field in fields:
    # Check the field name, perform a calculation when finding the field 'Flag'
    #
    if field.name == "Flag":
        # Set the value for the field and exit loop
        #
        arcpy.CalculateField_management(fc, "Flag", "1")
        break

The properties of the field and index objects are listed below:

Property

Explanation

Name

The name of the field.

AliasName

The alias name of the field.

Domain

The name of the associated domain.

Editable

True if the field is editable.

IsNullable

True if the field is nullable.

Required

True if the field is required.

Length

The field's length.

Type

SmallInteger, Integer, Single, Double, String, Date, OID, Geometry, BLOB.

Scale

The field's scale.

Precision

The field's precision.

Field properties

Property

Explanation

Name

The name of the index.

IsAscending

True if the index is sorted in ascending order.

IsUnique

True if the index is unique.

Fields

A Python list of field objects. This is the same as using Describe's field property.

Index properties

TipTip:

ListFields and ListIndexes can be used to limit the results based on name and type.

Related Topics


12/15/2011