I'm new to ArcPy.

I'm trying to write a script that will loop through the unique values in a field in a FC and create dynamic layers, with definition queries, for each unique value.

  • Each of the dynamic layers would all point to the same master feature class.
  • I do not want to output static feature classes. For example, I can't use the split by attributes tool for this.

I've attempted to adapt an ArcPy script from a related answer to do it:

from arcpy import *

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
fc = "WO_VW_FGDB"

env.overwriteOutput = True

#Create cursor to iterate rows
cursor = da.SearchCursor (fc, field)
for row in cursor:
    #sql statement for a single feature
    sql = '"{0}" = \'{1}\''.format (field, row[0])
    #Make layer with sql for one feature only. Use the field value as the layer name.
    MakeFeatureLayer_management (fc, row[0], sql)   
    #Make mapping layer object
    lyr = mapping.Layer(row[0])
    #Add mapping layer object to map
    mapping.AddLayer (df, lyr)
del cursor
mxd.save ()

When I run the script in ArcGIS Desktop 10.7.1, it successfully creates the first layer. In fact, it actually creates duplicate layers.

But then it produces an error:

Runtime error  Traceback (most recent call last):   File "<string>", line 17, in <module>   
File "c:\program files (x86)\arcgis\desktop10.7\arcpy\arcpy\management.py", line 6986, in MakeFeatureLayer     
raise e ExecuteError: ERROR 000622: Failed to execute (Make Feature Layer). Parameters are not valid. 
ERROR 000628: Cannot set input into parameter in_features. 

What am I doing wrong?

  • 1
    Post you referred deals with city names that are supposedly unique. You need to create unique list of values and iterate through it.
    – FelixIP
    Commented Aug 23, 2020 at 20:31
  • 2
    Unique vals can be generated using a python set. i.e uniquevals = set((row[0] for row in cursor)) which you can then loop through i.e for val in uniquevals"
    – user2856
    Commented Aug 23, 2020 at 21:59

2 Answers 2


First you need to create a list of unique field values. Use a function like the one below:

def unique_values(table , field, criteria = None):
with arcpy.da.SearchCursor(table, [field], criteria) as cursor:
    vals = set({row[0] for row in cursor})
    if (None in vals):
    if ('' in vals):
    return sorted(vals)

Then as a seperate step loop through those values creating your layers. E.g.

vals = unique_values(fc)
for v in vals :
    sql = '"{0}" = \'{1}\''.format (field, v)
    MakeFeatureLayer_management (fc, v, sql)   
  • Thanks, Anna. You might be interested in what I came up with (I posted an answer below). Cheers.
    – User1974
    Commented Aug 26, 2020 at 15:36

This script works in ArcGIS Pro 2.4 (but not ArcGIS Desktop 10.7.1.):

def DuplicateLayer(CopyLayer, field, uniquevalue):
    p = arcpy.mp.ArcGISProject("Current")
    m = p.listMaps("Map")[0]
    LayerToDup = m.listLayers(CopyLayer)[0]
    m.addLayer(LayerToDup, "AUTO_ARRANGE")
    LayerToDup.name = str(CopyLayer)
    lyr = m.listLayers(CopyLayer)[0]
    lyr.definitionQuery = field + " = '" + uniquevalue + "'"
    lyr.name = uniquevalue
    print (lyr.definitionQuery)
def unique_values(layer, field):
    with arcpy.da.SearchCursor(layer, [field]) as cursor:
        return sorted({row[0] for row in cursor})

def ExplodeFC(layername,field):
    fieldvals = unique_values(layername,field)
    for value in fieldvals:

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.