How to iteratively select by unique field values and create new feature

Jump to solution
01-22-2021 01:26 PM
Labels (1)
New Contributor II


I am trying to iteratively select by attribute>create feature layer> create new feature class(es) from the selections. But I am getting errors in the Make Feature Layer process.  Any suggestions?

Thanks, Penelope 

Update: Edited for code formatting--thanks for the tip.



#obtain unique values

values = [row[0] for row in arcpy.da.SearchCursor(SortiesLnFP, 'RS_Name')]
uniqueValues = set(values)




#create where clause for .makefeaturelayer using unique Values
#use where query to make a feature layer from selection then convert to feature class

OutPath = wkspc
for value in uniqueValues:
    OutFile = wkspc + os.sep + str(value)
    query = "\"RS_Name = '{}'\"".format(value)
    #query = "RS_Name = " + str(value)
    sortieLayer =, OutFile, query)
    arcpy.conversion.FeatureClassToFeatureClass(sortieLayer, wkspc, str(value))




#Output of query
"RS_Name = 'Pink_PM'"
"RS_Name = 'Pink2'"
#i've tried w/ the "" and without, get the same error either way-- I saw another post including those for the same purpose so thought I would try it (


ExecuteError                              Traceback (most recent call last)
In  [67]:
Line 6:     sortieLayer =, OutFile, query)

File C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\, in MakeFeatureLayer:
Line 8668:  raise e

File C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\, in MakeFeatureLayer:
Line 8665:  retval = convertArcObjectToPythonObject(gp.MakeFeatureLayer_management(*gp_fixargs((in_features, out_layer, where_clause, workspace, field_info), True)))

File C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\, in <lambda>:
Line 511:   return lambda *args: val(*gp_fixargs(args, True))

ExecuteError: ERROR 000358: Invalid expression
Failed to execute (MakeFeatureLayer).


0 Kudos
1 Solution

Accepted Solutions
MVP Esteemed Contributor
4 Replies
MVP Esteemed Contributor

Code formatting ... the Community Version - GeoNet, The Esri Community

will help make your code more readable 


sortieLayer =, OutFile, query)

case difference !

... sort of retired...
MVP Frequent Contributor

Agreed, the code is very hard to read and unformatted.

No idea if the rest is any good, but it seems that your query is enclosed in another set of quotes, remove those and it should be valid (if value is a string, if its a number - remove the single quotes also).


query = "RS_Name = '{}'".format(value)
0 Kudos
MVP Esteemed Contributor

of course there is always

Split By Attributes (Analysis)—ArcGIS Pro | Documentation

... sort of retired...
New Contributor II

Thank you very much @DanPatterson for pointing me in the right direction. I cannot believe I've never used that tool, very useful! 



0 Kudos