HOW TO
In ArcGIS Pro, organizing layers within the Contents pane helps improve map management and visualization. Moving a feature layer into a group layer organizes related datasets. While this can be performed manually, automating the process using Python improves efficiency, particularly when working with multiple layers.
Complete the workflow provided to move a feature layer into a group layer within the Contents pane using Python in ArcGIS Pro.
import arcpy
aprx = arcpy.mp.ArcGISProject("CURRENT") mp = aprx.listMaps()[0]
#For single layer layer_names = ["<Layer_Name>"] #For multiple layers layer_names = ["<Layer_Name>", "<Layer_Name>", "<Layer_Name>"] layers_to_move = [lyr for lyr in mp.listLayers() if lyr.name in layer_names]
group_list = mp.listLayers("<Group_Name>") group_layer = group_list[0]
for lyr in layers_to_move: mp.addLayerToGroup(group_layer, lyr, "AUTO_ARRANGE") print(f"Layer '{lyr.name}' successfully moved into group '{group_layer.name}'.")
Note: This script uses auto arrange for layer placement. To change the placement of the added layer within the group layer, refer to ArcGIS Pro: Methods for instructions and more information.
The code below demonstrates the full working script to move the feature layers into the group layer.
import arcpy aprx = arcpy.mp.ArcGISProject("CURRENT") mp = aprx.listMaps()[0] layer_names = ["TestLayer1", "TestLayer2", "TestLayer3"] layers_to_move = [lyr for lyr in mp.listLayers() if lyr.name in layer_names] group_list = mp.listLayers("TestGroupLayer") group_layer = group_list[0] for lyr in layers_to_move: mp.addLayerToGroup(group_layer, lyr, "AUTO_ARRANGE") print(f"Layer '{lyr.name}' successfully moved into group '{group_layer.name}'.")
Article ID: 000034956
Get help from ArcGIS experts
Download the Esri Support App