I want to extract the index of a field with its name using arcpy. It seems what I have written is so dumb. Is there any simpler method with out using for-loop?

    import arcpy

    fieldList = arcpy.ListFields(self.address_ref_layer1)
    self.field_name = []
    for field in fieldList:
    print self.field_name
    print self.field_name.index(u"NAMN1")

It could also be written:

    fieldList = arcpy.ListFields(self.address_ref_layer1
    name_index = [f.name for f in fieldList].index(u"NAMN1")

The ListFiled function returns list of field objects with many properties as well as name, but no function to return the index of the field which has a specific name?


You might want to investigate the FieldMappings object, which has various methods which deal with indices.

Instead of using for loops, you can use list comprehensions, like so:

self.field_name = [field.name for field in fieldList]

It can be shortened even further, since you may not need to store fieldList at all:

self.field_name = [field.name for field in arcpy.ListFields(self.address_ref_layer1)]
| improve this answer | |
  • I have never used FieldMappings, but as I understand it is mainly used for merging data sets. – msc87 Jun 10 '14 at 17:16
  • 1
    @msc87, yes it is generally used for that. But it's likely the easiest way to concisely get all the information about fields that you'll want. – Paul Jun 10 '14 at 17:18

This Python function will return the Index Name associated with the FeatureClass and Field Name passed in.

def getIndexName(fieldname,fc):
    indexes = arcpy.ListIndexes(fc)
    for index in indexes:
        for f in index.fields:
            if f.name.upper() == fieldname.upper():
                return index.name
| improve this answer | |
  • Hi Klewis , i am trying to use your code to get the fieldindex. Why do i get an AttributeError: no attribute 'upper' ? – Jio Aug 17 '15 at 8:26

I struggled to find an answer for this on the ESRI sites which is why I ended up here. The clue is the object has field.

import arcpy
from arcpy import env
FC = r'Your_FC'
indexes = arcpy.ListIndexes(FC)
    for index in indexes:
        for fieldname in index.fields:
        fieldname = fieldname.name
        indexname = index.name
        print fieldname, indexname
| improve this answer | |

You can also create a function that will return the index of a field:

import arcpy
def findindex(table,fieldname):
    return [i.name for i in arcpy.ListFields(table)].index(fieldname)

Input a feature class or table and the field 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.