Convert cursor row to dictionary on the fly

7291
0
03-15-2018 02:53 PM
BlakeTerhune
MVP Regular Contributor

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'])‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Replies