0

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
 cursor.updateRow(row)
 print "Processing perf flags for " + rastername
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Dec 1, 2018 at 12:59
1
  • 3
    Python Array Use 101: indexes start at zero, and you only provide four field names, so row[4] is out of range. Commented Dec 1, 2018 at 13:05

1 Answer 1

3

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.

answered Dec 1, 2018 at 13:54
1
  • 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! Commented Dec 2, 2018 at 4:39

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.