1

I am trying to update all values in column in a feature layer in ArcGIS Pro using arcpy.da.UpdateCursor, but I am getting the error: IndexError: list index out of range

I am trying to update all current values in the column "Dist_Next" with the values in the list distance_next_stop, i.e. the first row should be replaced with 19.32..., the second row with 73.84..., etc. The feature layer and the list contains the same amount of rows. What am I doing wrong here?

distance_next_stop = [19.320592048800002, 73.8492150239, 162.16051086800002, 0.361037751915, 162.295311589, 0]
with arcpy.da.UpdateCursor("out_stops", "Dist_Next") as cursor:
    for row in cursor:
        row = distance_next_stop[i]
        cursor.updateRow(row)

The column "Dist_Next" in the feature layer "out_stops" that I want to update:

The column "Dist_Next" in the feature layer "out_stops" that I want to update

3
  • Your code doesn't declare or alter i. Try putting parens or braces around row, since a list is expected in the updateCursor method. Note that it isn't safe to assume that rows will always be processed in physical order. Commented Oct 26, 2021 at 14:59
  • In a specific line, or using (row) or {row} in line 3, 4, and 5? Commented Oct 26, 2021 at 15:07
  • Regarding declaring and altering, does that mean doing something like: for row in cursor: i = 0 row = distance_next_stop[i] i += 1 cursor.updateRow(row) Commented Oct 26, 2021 at 15:28

1 Answer 1

2

Use zip:

import arcpy

distance_next_stop = [19.320592048800002, 73.8492150239, 162.16051086800002, 0.361037751915, 162.295311589, 0]
with arcpy.da.UpdateCursor("out_stops", "Dist_Next") as cursor:
    for row, dist in zip(cursor, distance_next_stop):
        row[0] = dist
        cursor.updateRow(row)

Or enumerate:

...
for i, row in enumerate(cursor):
    row[0] = distance_next_stop[i]
...
2
  • 1
    Thank you! That did the trick - much appreciated! Commented Oct 26, 2021 at 16:47
  • 1
    Nice! Please accept my answer with the checkbox Commented Oct 26, 2021 at 18:54

Your Answer

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.