I have a python code which iterates through all attribute fields and creates a map for each field automatically. However, in the code I have to specify each attribute manually which was fine so far. Now I have an attribute table with many fields and I need a way to do the same without specifying the attributes manually (as done with "Attrlist" in the code. Any suggestions?

This is the code I am using so far:

outPath = "D:\\Atlas\\test\MxdMaps\\Mxd_MapAutomation\\"   
# list of attributes with the same datatype
attrlist = ["EthnicGroups_totnumberoffarmhh_txt_WNHDeAA01", 
# layer which has to change the attribute field
featurelayer = arcpy.mapping.ListLayers(mxd, "EthnicGroups_totnroffarmhh")[0]
# text element that has to change (e.g. title...)
title = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "map_title")[0]
for attr in attrlist:
        #if featurelayer.symbologyType == "GRADUATED_COLORS":
                #print attr
                # change value field
                featurelayer.symbology.valueField = attr
                #print "Break values: %s" % featurelayer.symbology.classBreakValues
                # change tilte
                title.text = attr
                # update view
                # export as jpeg
                outJPEG = os.path.join(outPath, attr + ".jpg")
                arcpy.mapping.ExportToJPEG(mxd, outJPEG, "PAGE_LAYOUT", 200, 200, 300, False, "24-BIT_TRUE_COLOR", 90, False)
                # export as AdobeIllustrator .ai
                #outAI = os.path.join(outPath, attr +  ".ai")
                #arcpy.mapping.ExportToAI(mxd, outAI, "PAGE_LAYOUT", 200, 200, 300, "BEST", "RGB", "VECTORIZE_BITMAP", False)
del mxd
  • 2
    arcpy.ListFields(featurelayer,"*","String") resources.arcgis.com/en/help/main/10.1/index.html#//… will give you all the string fields in a feature class - is that what you're after? Commented May 8, 2015 at 2:15
  • I think you should accept the answer to your first question and then edit out your second question because it should be researched/asked separately. The desirability of one question per question is mentioned in the Tour.
    – PolyGeo
    Commented May 8, 2015 at 11:06

1 Answer 1


arcpy.ListFields() will give you a list of objects. You just need to get the names from those objects.

field_names = [f.name for f in arcpy.ListFields(lyr)]

Another round about way of getting them would be via a search cursor but that also includes the shape field.

field_names = []
with arcpy.da.SearchCursor(lyr,"*") as cursor:
    field_names = cursor.fields
  • Thanks! I am almost there! Can you also tell me how I would change the code if I wanted to leave out like the first 20 fields and the last 2 fields from the List? Commented May 19, 2015 at 2:32
  • field_names[20:len(field_names)-2]
    – JasonT
    Commented May 19, 2015 at 2:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.