[GIS] Using arcpy.da.UpdateCursor with dictionary

arcgis-10.2arcpycursordictionarypython-2.7

I'm working through some da.UpdateCursor scripts to make updates to a fgdb. Although the script below works successfully, I want to use the dictionary that I have created rather than the manual [ ] system I have for demo purposes.

Can anyone provide some guidance please as I have researched this quite significantly and can't get an answer after some hours of effort and no error message. (Using Win 7, ArcGIS 10.2, Python 2.7.5)

The code below updates 'book_ref' where it finds the matching 'book_id' in 'seq = …….'

    seq = [[7643, 7625], [9644, 2289]]
    Att_Dict = {}
    for pair in seq:
        Att_Dict[pair[0]] = pair[1]

    with arcpy.da.UpdateCursor(in_fc1, ['book_id', 'book_ref']) as cursor:
        for row in cursor:
            pozzy = row[0]
            if pozzy in Att_Dict:
                row[1] = Att_Dict[pozzy]
                cursor.updateRow(row)

How do I get it to use the values from my dictionary?

seq = {7643: 7625, 
       9644: 2289, 
       4406: 4443, 
       7588: 9681, 
       2252: 7947}

Best Answer

To test your code I created a test feature class with two long integer fields:

enter image description here

and ran this code:

import arcpy

in_fc1 = r"C:\Temp\test.gdb\PointFC"

seq = [[7643, 7625], [9644, 2289]]
Att_Dict = {}
for pair in seq:
    Att_Dict[pair[0]] = pair[1]

with arcpy.da.UpdateCursor(in_fc1, ['book_id', 'book_ref']) as cursor:
    for row in cursor:
        pozzy = row[0]
        if pozzy in Att_Dict:
            row[1] = Att_Dict[pozzy]
            cursor.updateRow(row)

which produced the expected result of:

enter image description here

Related Question