I am trying to use an update cursor and receiving an error. I'm not very familiar with using cursors, so please bear with me. I am trying to write a 1 or 0 to a field, based on a condition. The error I am receiving is:

IndexError: list assignment index out of range.

I have googled around and have seen solutions, but I'm still not understanding why its throwing an error.

#Define Environments
rasters = r"C:\Data.gdb"
points = r"C:\Data.gdb\Points"

#Set workspace
arcpy.env.workspace = rasters
arcpy.env.overwriteOutput = True
# Get a list of the rasters in the workspace  
rasters = arcpy.ListRasters()
# Loop through the list of rasters  
for inRaster in rasters:  
    print str("Processing " + inRaster)
    rastername = str(inRaster)
    fieldname = str(inRaster + "_perf_flag")
    # Add surface information to Well FC
    arcpy.AddSurfaceInformation_3d(points,inRaster , "Z", "LINEAR")
    print "Formation Depth Added"
    # Create positive values from SS values
    arcpy.CalculateField_management(points, "Z", '!Z!*-1', "PYTHON")
    print "Depth Values Made Positive"
    # Add new field name for perf flag
    arcpy.AddField_management(points, fieldname, "LONG", 9, "", "", fieldname, "NULLABLE", "NON_REQUIRED")
    print "Flag Field Added"
    # Raname Z field as formation grid name
    arcpy.AlterField_management(points, 'Z', rastername, rastername)
    print "Field Altered"
    # Calculate if well was perfed in the formation - Flag 1 as yes - Flag 0 as no
    with arcpy.da.UpdateCursor(points, ["Base", "Top",rastername, fieldname]) as cursor:
        for row in cursor:
            if (row[3] >= row[0] and row[3] <= row[1]):
                row[4] = 1
            if not (row[3] >= row[0] and row[3] <= row[1]):
                row[4] = 0
        print "Processing perf flags for " + rastername
  • 3
    Python Array Use 101: indexes start at zero, and you only provide four field names, so row[4] is out of range.
    – Vince
    Dec 1 '18 at 13:05

Your update cursor has four values in its row: ["Base", "Top",rastername, fieldname] corresponding to row[0],row[1],row[2],row[3] respectively. row[4] is out of range because it is the fifth of four items.

  • Thanks for the help.I'm not sure how I missed that, as I accounted for row[0].. I adjusted the script and it ran as expected. Thanks again for looking it over!
    – phead
    Dec 2 '18 at 4:39

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.