[GIS] Getting unique value of second field using arcpy.da.SearchCursor

arcpycursor

I have a feature class and using arcpy.da.SearchCursor I'm getting unique values of a field (e.g. Name). How can I use this method to get the associated row value of a different field (diff field = Name2)?

So far I use this to get unique values on only field Name:

values = [row[0] for row in arcpy.da.SearchCursor(fc, ("Name"))]
    uniqueValues = set(values)
    for value in uniqueValues:  

When searching through the feature class how do I also get the value of another field, field Name2?

Best Answer

Dictionary comprehension:

{row[0]: row[1] for row in arcpy.da.SearchCursor(fc, ("Name", "Name2"))}

Note this will only have ONE value per unique Name column value. You can also use collections to get a list of Name2 values:

import collections
names = collections.defaultdict(list)

for name1, name2 in arcpy.da.SearchCursor(fc, ("Name", "Name2")):
    names[name1].append(name2)