1

I'm trying to extract a list of unique values in two fields in a shapefile. I think I am misunderstanding how the search cursor works, because I get the same error on which ever variable (x or y) I put second.

I'm following the answer here, and it works for just one field, but not 2+

Code:

with arcpy.da.SearchCursor(myshp, ['myid', 'myname']) as cursor:
    x = sorted({row[0] for row in cursor})
    y = sorted({row[1] for row in cursor})

Error:

y = sorted({row[1] for row in cursor})
IndexError: list index out of range
2

You can use a generator expression for this. For example:

import arcpy

fc = r'C:\path\to\geodatabase.gdb\featureclass'

unique_list1 = set(row[0] for row in arcpy.da.SearchCursor(fc, 'myid'))
unique_list2 = set(row[0] for row in arcpy.da.SearchCursor(fc, 'myname'))

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.