8

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?

  • 1
    Can you provide a little more context? What are you looking to do exactly? – blah238 Feb 4 '13 at 20:46
8

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)
2

The help file on SearchCursor (arcpy.da) has Code Samples at the bottom. The first SearchCursor example 1 shows how to grab a list of fields. Assign the row[0] and row[1] to variable names to use in your code.

  • @con, The list of fields .format row method did not work for, for value in uniqueValues: – artwork21 Feb 5 '13 at 15:20
  • 2
    @artwork21 Can you post the modified code as an edit to your additional question? – Fezter Feb 6 '13 at 4:33

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.