blake.terhune

Convert cursor row to dictionary on the fly

Discussion created by blake.terhune Champion on Mar 15, 2018

Thanks to Jibin Liu and Ghislain Prince for presenting this little gem at the 2018 Dev Summit in their session "Python: Working with Feature Data." This is an easy method of allowing you to access fields in your cursor by name instead of index number.

def row_as_dict(cursor):
    for row in cursor:
        yield dict(zip(cursor.fields, row))

with arcpy.da.SearchCursor(table, fields) as cursor:
    for row in row_as_dict(cursor):
        print(row['RoadName'])

Outcomes