Table of Contents
Introduction
A common task is to get a list of unique attribute values for a field in a feature class or table either for a report or for continuing a a workflow with ArcPy. In this blog post we will look at two alternate ways to use the Search Cursor to return a list of unique values.
Mastering ArcGIS Pro ArcPy Search, Insert, & Update Cursors
Unlock the full potential of ArcPy Cursors with our intensive and in-depth course. Designed for GIS professionals, analysts, and enthusiasts, this course delves into the intricacies of Search, Insert, and Update Cursors in the ArcPy library, empowering you to manipulate and manage spatial data with precision and efficiency.
ArcPy Cursors enable you to streamline your GIS workflows, automate repetitive tasks, and witness the full potential of spatial data manipulation in ArcGIS Pro.
Import the ArcPy module
ArcPy is a Python site package that enables automation, analysis, and management of geographic data within the ArcGIS software environment.
import arcpy
Required input
We need the feature class (or table) filepath along with the name of the field that we wish to retrieve unique values for.
## the feature class or table that contains the field
input_tbl = r"path\to\featre_class"
## the field name to get unique values for
field_name = "FIELD_NAME"
#1 List Comprehension and the SearchCursor
In the example below we use list comprehension along with the SearchCursor to return a list of unique values. We utilise the sql_clause parameter from the SearchCursor to limit the return to unique (DISTINCT) values only.
unique_values = [
row[0] for row in arcpy.da.SearchCursor(
in_table = input_tbl,
field_names = field_name,
sql_clause=("DISTINCT", None)
)
]
print(unique_values)
#2 Set Comprehension and the SearchCursor
In the example below we use set comprehension along with the SearchCursor to get our unique values. We could leave the return as a set, but below, we cast the set returned as a list(). Notice we do not use the sql_clause with this example. A Python set can only contain unique values so the sql_clause is redundant here.
unique_values = list({
row[0] for row in arcpy.da.SearchCursor(
in_table = input_tbl,
field_names = field_name
)
})
print(unique_values)
Want a sorted list?
If you want to have a sorted list returned you can wrap the list comprehension using the built-in sorted() function.
unique_values = sorted([
row[0] for row in arcpy.da.SearchCursor(
in_table = input_tbl,
field_names = field_name,
sql_clause=("DISTINCT", None)
)
])
And you can do the same for the list() call when using the set comprehension method.
unique_values = sorted(list({
row[0] for row in arcpy.da.SearchCursor(
in_table = input_tbl,
field_names = field_name
)
}))
NOTE: the sorted() function will fail with a TypeError if there are NULL values in your field! You will need to accounf for this.
Mastering ArcGIS Pro ArcPy Search, Insert, & Update Cursors
Unlock the full potential of ArcPy Cursors with our intensive and in-depth course. Designed for GIS professionals, analysts, and enthusiasts, this course delves into the intricacies of Search, Insert, and Update Cursors in the ArcPy library, empowering you to manipulate and manage spatial data with precision and efficiency.
ArcPy Cursors enable you to streamline your GIS workflows, automate repetitive tasks, and witness the full potential of spatial data manipulation in ArcGIS Pro.