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:

        # add value to valueset for checking against in next iteration

    # return value list
    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!

  • Jason Scheirer

    You’re doing quite a bit of extra work you don’t need to. First off, you don’t need to check if an item is in a set before you add it. Second, you don’t need that extra list, you can use the sorted() builtin to contruct it at the end. The simplified code would look something like this:

    def getValueList (inputTable, field):
        values = set()
        rows = arcpy.SearchCursor(inputTable)
        for row in rows:
        return sorted(values)
    • Joel

      Excellent… thank you, Jason. I implemented this cleaned up version of the function today in my script.

    • Craig

      This is a very useful bit of code and I want to use it for placing values into a definiton query. How do I reference the generated list and see if it has populated correctly? Have only used python briefly for minor functions.

  • Amy Smith

    Thanks Joel and Jason. This script came in very handy for me today!

  • christi nelson

    Hi guys,

    What if I want to create a list of unique table values for more than one field? What would the syntax look like?

    • Joel

      What are you trying to put together Christi? Do you want to concatenate these values together, or do you want to create a composite definition query for layers?

  • Rhett

    I just use arcpy.Frequency_analysis(merged_sorted, merged_delta, “SSTE_NUM”, “”)

    Gives you a new table with all the values and number of times they occur in the data (like pivot table count).


    • Joel

      True Rhett. However, I wanted to design this script to not write anything to disk, running entirely in RAM for the sake of efficiency. The Frequency tool writes a table to disk, something I wanted to avoid.

  • Pingback: Create List of Unique Table Values using Data Access Module | Joel McCune

  • Pingback: Data Conversion Tools for Data Driven Pages | Dori's GIS Portfolio

  • Pingback: Create List of Unique Table Values using Python for ArcGIS Analysis | All Around GIS