How do I convert the lyr variable into a list? The variable type is listed as class 'arcpy._mp.Layer'. Should I convert this to string then insert into list?
import arcpy aprx = arcpy.mp.ArcGISProject("./test.aprx") for m in aprx.listMaps(): for lyr in m.listLayers(): print (lyr) output layer1 layer2 layer3 desired output ['layer1', 'layer2', 'layer3'] ex: 2
for m in aprx.listMaps(): for lyr in m.listLayers(): layer = [str(lyr)] print (layer) output ['layer1'] ['layer2'] ['layer3'] This doesn't seem to get into a list either
1 Answer
m.listLayers() already returns a list of Layer objects of type arcpy._mp.Layer. You just need to build a list of layer names. Here's how you can do that:
import arcpy aprx = arcpy.mp.ArcGISProject("./test.aprx") map_layers = [] # list of lists where each sublist contains layer names for each map in project for m in aprx.listMaps(): lyr_names = [] # initialize an empty list and append layer names to it in every iteration for lyr in m.listLayers(): lyr_names.append(lyr.name) map_layers.append(lyr_names) print(map_layers) So if your Project had 2 maps where each map had 3 layers each, the above snippet would print the result like this:
[['Map 1 Layer 1 Name', 'Map 1 Layer 2 Name', 'Map 1 Layer 3 Name'], ['Map 2 Layer 1 Name', 'Map 2 Layer 2 Name', 'Map 2 Layer 3 Name']]