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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 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!

11 thoughts on “Create List of Unique Table Values using Python for ArcGIS Analysis”

  1. 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:
            values.add(row.getValue(field))
        return sorted(values)

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

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

  2. 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?

    1. 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?

  3. 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).

    R_

    1. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>