Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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?

Thanks

share|improve this question
1  
Can you provide a little more context? What are you looking to do exactly? –  blah238 Feb 4 '13 at 20:46
add comment

2 Answers

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)
share|improve this answer
add comment

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.

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.