Here's a stab at my first practical ArcPy script.

I want to grab a layer by name (rather than by layer order or by file path) in the current MXD, then loop over it's attributes (with a view to modifying them). However I don't know the syntax of connecting the desired layer to the attribute table

Here's what I've got so far:

import arcpy

mxd = arcpy.mapping.MapDocument("CURRENT")
needle = "1km_Grid"
df = arcpy.mapping.ListDataFrames(mxd)[0]
for lyr in arcpy.mapping.ListLayers(mxd, "", df):
    if lyr.name == needle:
    # curObj = this layer # pseudo code!

columns = ["MID", "Shape"]

for row in curObj:
    print ("column 1 is: %s" % (row[0]))

1 Answer 1


After you get the lyr.name, use the Update Cursor to loop over the attributes and make changes as needed.

if lyr.name == needle:
    with arcpy.da.UpdateCursor(lyr, columns) as cursor:
        for row in cursor:
            print "column 1 is: {}".format(row[0])
  • For the win how do I then update the attribute table? arcpy.Buffer_analysis(columns) doesn't work. Commented Jul 11, 2016 at 13:14
  • The buffer method buffers features not table attributes. If you want to run the buffer method on the layer you do not need to use the cursor. If you need to run the buffer on only certain features within the layer you will need to convert the layer to a feature layer and then make a selection on the layer/features then use the buffer method. Note, that buffer method creates a new layer.
    – artwork21
    Commented Jul 11, 2016 at 13:46
  • Ahh, my confusion I thought you needed to run than after updating a table. Commented Jul 11, 2016 at 14:53

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.