My apologies if this question has been asked before. I've spent some time searching for a similar question with no results.
Is it possible to add each value under X column to a list using ArcPy? I think I could brute force it by using cursor and writing each to a list but I'm hoping there is an easier way.
I'll admit I'm fairly new to Python (and ArcPy) but I'm trying to explore the possibilities with several ideas I have in mind.
2 Answers 2
Assuming you have access to ArcMap> 10.1, this is a one liner that will write your field information to a list.
fname = "field1"
fdata = [row[0] for row in arcpy.da.SearchCursor(shp, fname)]
The code below will work if fname
is a list. The resulting fdata
will be a list of lists.
fname = ["field1", "field2", "field3"]
fdata = [[r for r in row] for row in arcpy.da.SearchCursor(shp, fname)]
I've assumed that you are using ArcGIS for Desktop 10.1 or 10.2 so the function below should work to create a list of all values in a field using the Data Access module of ArcPy. The last line just shows an example of the function being called.
import arcpy
def all_values(table, field):
return [row[0] for row in arcpy.da.SearchCursor(table, [field])]
allValuesList = all_values(r"C:\polygeo\QGIS\IQ_QLD_CADASTRETENURE_DCDB_CUR.shp","TENURE")
Inspiration for this code came from GIS Stack Exchange and ArcPy Cafe .