AnsweredAssumed Answered

How to use update cursor with a dictionary?

Question asked by rhowell2 on Mar 7, 2019
Latest reply on Jul 16, 2019 by bixb0012

Newbie here.

 

I'm self-teaching about cursors and am practicing on this dataset. "testclass" is a feature class with an ID field (6 digit integer) and a name field. I want to go through and assign a unique name based on the ID. The following code works and illustrates more or less what I am trying to do (although I am sure it gives many of you heartburn with how clunky it is-- sorry ).

point = "testclass"
update = arcpy.da.UpdateCursor(point,["ID", "name"])

for row in update:
    ID = row[0]
    if (ID == '174769'):
        name = "R4S8f"
        row[1] = name
    elif (ID == '174771'):
        name = "C3F8m"
        row[1] = name
    elif (ID == '174773'):
        name = "F4S8m"
        row[1] = name
    elif (ID == '174774'):
        name = "C1S8f"
        row[1] = name
    elif (ID == '174776'):
        name = "F3S8f"
        row[1] = name
    elif (ID == '174778'):
        name = "R1S8f"
        row[1] = name
    elif (ID == '174781'):
        name = "R6F8f"
        row[1] = name

    update.updateRow(row)

I would like to take this same concept and use an update cursor with a dictionary. I've found several posts online so I think I'm close but I can't quite get the syntax. This is what I have.

assignName = {174769 : 'R4S8f', 174771 : 'C3F8m', 174773 : 'F4S8m', 174774 : 'C1S8f',
              174776 : 'F3S8f', 174778 : 'R1S8f', 174781 : 'R6F8f'}

cursor = arcpy.da.UpdateCursor('testclass',["ID", "name"])

for row in cursor:
    if row.ID in assignName:
        updateRow.name = assignName[updateRow.ID]
        update.updateRow(row)

It's telling me in line 7 "'list' object has no attribute 'ID'" when I run this in ArcGIS Pro.

 

Thanks in advance

Outcomes