Create List of Unique Table Values using Python for ArcGIS Analysis
NOTE: I have updated this function for 10.1 and consolidated some of the code. The sample is available in a new post, Create List of Unique Table Values using Data Access Module.
Necessity is the mother of invention and as of late, this means I have learned to use Python with ArcGIS. Early on, an issue I ran across was needing a list of all unique values in a field to be used in further analysis. Unfortunately there is not a pre-built method for this. Still, it did not take long to figure out the following function to accomplish this.
# Return list all permutations of field in input feature class or table def getValueList (inputTable, field): valueList =  # array to hold list of values collected valueSet = set() # set to hold values to test against to get list rows = arcpy.SearchCursor(inputTable) # create search cursor # iterate through table and collect unique values for row in rows: value = row.getValue(field) # add value if not already added and not current year if value not in valueSet: valueList.append(value) # add value to valueset for checking against in next iteration valueSet.add(value) # return value list valueList.sort() return valueList
Hopefully if you have run across this problem, you find this useful. If you know of a better method, please comment and let me know!