1

I am trying to calculate new records in several fields by looping through each field in a list and dividing the value of each record by its shape area. My code is as follows:

inFeature = outFeature
my_list = ['a', 'b', 'c']
divfield = "Shape_Area"

with arcpy.da.UpdateCursor(inFeature, my_list) as cursor:
    for row in cursor:
        for n in range(len(my_list)):
            row[n] = row[n] / row[divfield]
        cursor.updateRow(row)

I am getting the error: TypeError: list indices must be integers, not str

All of my fields are stored as double.

2

List indices must be integers, not strings.

Where you use row[divfield] you are trying to use "Shape_Area" (the value of divfield), which is a string, as a list index.

You have opened your cursor with three fields 'a', 'b' and 'c' which you can refer to the values of using row[0], row[1] and row[2] respectively.

0

This is what I ended up using and it worked out:

inFeature = outFeature
my_list = ['a', 'b', 'c', 'Shape_Area']
divfield = len(my_list) - 1

with arcpy.da.UpdateCursor(inFeature, my_list) as cursor:
    for row in cursor:
        for n in range(len(my_list)):
            row[n] = row[n] / row[divfield]
        cursor.updateRow(row)

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.