20

I have a number of columns in a number of tables withinh a FGDB where I need to extract the unique values for each column.

For Example: the values may be [1,2,2,2,3,4], and I am trying to return [1,2,3,4]

I could do this job a number of other ways in ARCGIS but I am trying to extend myself.

I have found a piece of python on the web that I think will do the job but I am struggling to get it to run (I keep getting an invalid syntax error as I keep getting the syntax error in line 3) this will no doubt be a really simple user error.

Code Snippet below

import arcpy

def unique_values(r'N:\GISProjects\Landuse\Plant_Biosecurity_Project\ArcGIS_Online.gdb\Holdings_Property_Merge' , 'LU_ALUMMaj'):
with arcpy.da.SearchCursor(table, [field]) as cursor:
    return sorted({row[0] for row in cursor})

This is the Error Message I get from sublime text:

 File "C:\Users\hawkinle\Desktop\STDTAS\Unique_Data.py", line 3
def unique_values(r'N:\GISProjects\Steve_Eastwood_Landuse\Plant_Biosecurity_Project\ArcGIS_Online.gdb\Holdings_Property_Merge' , 'LU_ALUMMaj'):
                                                                                                                             ^
SyntaxError: invalid syntax
[Finished in 0.1s with exit code 1]

Updates from Original Question

I have now updated my code with the answer provided by below but am recieving a secondary error.

New Code Snippet:

import arcpy

def unique_values(table , field):
    with arcpy.da.SearchCursor(table, [field]) as cursor:
        return sorted({row[0] for row in cursor})

myValues = unique_values(r'N:\\GISProjects\\Landuse\\Plant_Biosecurity_Project\\ArcGIS_Online.gdb\\Holdings_Property_Merge' , 'LU_ALUMMaj')

print (myValues)

I am getting a new error message related to a runtime error

Traceback (most recent call last):
File "C:\Users\hawkinle\Desktop\STDTAS\Unique_Data.py", line 7, in <module>
myValues = unique_values(r'N:\\GISProjects\\Steve_Eastwood_Landuse \Plant_Biosecurity_Project\\ArcGIS_Online.gdb\\Holdings_Property_Merge' , 'LU_ALUMMaj')
 File "C:\Users\hawkinle\Desktop\STDTAS\Unique_Data.py", line 4, in unique_values
with arcpy.da.SearchCursor(table, [field]) as cursor:
RuntimeError: cannot open 'N:\\GISProjects\\Steve_Eastwood_Landuse\\Plant_Biosecurity_Project\\ArcGIS_Online.gdb\\Holdings_Property_Merge'

[Finished in 8.0s with exit code 1]

I assume from the reading I have done that this relates to setting the env.workspace ?

Just showing Proof that it exisist

11
  • 1
    please edit your question to include your entire error message (as text)
    – Midavalo
    Commented Aug 25, 2016 at 23:59
  • Your new error shouldn't have anythign to do with the env.workspace I don't think. Try either taking off the r before the path, or changing the \` to ` in the path (and leave the r there). Does that Geodatabase exist?
    – Midavalo
    Commented Aug 26, 2016 at 1:06
  • Are you trying to isolate all of the unique values in a field? For example, say you have the following values [1,2,2,2,3,4], are you trying to return [1,2,3,4]. Please update the post to include this information.
    – Aaron
    Commented Aug 26, 2016 at 1:13
  • @Midavalo did you meant changing the path tolook like this ?r'N: GISProjects _Landuse Plant_Biosecurity_Project ArcGIS_Online.gdb Holdings_Property_Merge' , 'LU_ALUMMaj' Commented Aug 26, 2016 at 1:18
  • 1
    Thanks - fruther proof that im not very smart but I can lift heavy things. I owe you a speights!! Commented Aug 26, 2016 at 2:08

5 Answers 5

25

You've pretty much got it, you just need to specify the name of your parameters table and field in your function definition, and then pass those values when you call the function. Also watch your indentation, as it's vital for Python.

def unique_values(table , field):
    with arcpy.da.SearchCursor(table, [field]) as cursor:
        return sorted({row[0] for row in cursor})

myValues = unique_values(r'N:\GISProjects\Landuse\Plant_Biosecurity_Project\ArcGIS_Online.gdb\Holdings_Property_Merge' , 'LU_ALUMMaj')

print (myValues)

Basically this is saying that when you call the function unique_values() you'll pass values to two parameters, one called table, the other called field. These are then used in your function. When you call the function, in the line

myValues = unique_values(r'N:\GISProjects\Landuse\Plant_Biosecurity_Project\ArcGIS_Online.gdb\Holdings_Property_Merge' , 'LU_ALUMMaj')  

you are passing the values to these parameters.

This is the same as declaring your parameters separately and passing them to the cursor directly:

table = r'N:\GISProjects\Landuse\Plant_Biosecurity_Project\ArcGIS_Online.gdb\Holdings_Property_Merge'
field = 'LU_ALUMMaj'

with arcpy.da.SearchCursor(table, [field]) as cursor:
    myValues = sorted({row[0] for row in cursor})

print myValues
4
  • ahh that makes sense , when you include the individual paramters below I get what you are saying thankyou! . I am know moving up the chain of errors as I am getting a run time error which isnt allowing me to open the existing database. Ill update my question. Commented Aug 26, 2016 at 0:43
  • Why does the sorted () line return just one unique instance of each value when the sorted() function returns duplicates if you run it on a simple list like ['a', 'b', 'a', 'b']? I have been using this reply for some time and realized I did not understand why it works. Commented Jan 29, 2019 at 19:49
  • 2
    @DylanWarburg Because you're not passing a list of values, you are passing a set. If you add your list items above to a set, eg set(['a', 'b', 'a', 'b']), it will return unique values {'a', 'b'}. Using sorted() just returns them in sorted order as a simple set is unsorted.
    – Midavalo
    Commented Jan 30, 2019 at 3:10
  • If the field you are looking for unique values from is not a string, you should add: return sorted({str(row[0]) for row in cursor})
    – MKelly
    Commented Jun 6, 2019 at 15:55
12

I would advise using Python's built-in set() function along with a SearchCursor as a generator expression to find the unique values. You'll find this approach extremely efficient with large or small datasets:

import arcpy

fc = r'C:\path\to\your.gdb\featureclass'

unique_values = set(row[0] for row in arcpy.da.SearchCursor(fc, "some_field"))
0
5

The following approach was published on https://arcpy.wordpress.com/2012/02/01/create-a-list-of-unique-field-values/ It is using arcpy and numpy and has a smaller memory footprint than the SearchCursor approach.

import arcpy
import numpy

def unique_values(table , field):
    data = arcpy.da.TableToNumPyArray(table, [field])
    return numpy.unique(data[field]).tolist()

myValues = unique_values(r'N:\GISProjects\Landuse\Plant_Biosecurity_Project\ArcGIS_Online.gdb\Holdings_Property_Merge' , 'LU_ALUMMaj')

print (myValues)
2

Using arcpy.Frequency_analysis() quickly puts all the unique values from a field into a new table, which you can then use for cursor operations. One single command to do what all these other solutions do and faster and easier. As a bonus, you also get a count of how many times each value appears.

0

open Python window in Arcgis Pro. make sure you have layer in ToC and paste this print(*sorted(list(set(row[0] for row in arcpy.da.SearchCursor("layer name", "Column Name")))), sep="\n")

Source: https://www.youtube.com/watch?v=9Ey9NUNaP7k&t=3s

1
  • The list function is unnecessary since sorted - Python 3.11 accepts an iterable and sets are iterable.
    – bixb0012
    Commented Dec 22, 2022 at 13:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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