HOW TO

List selected layers from an ArcGIS Pro map using ArcPy

Last Published: June 20, 2024

Summary

An ArcGIS Pro map consists of multiple layers, each representing different datasets. Prioritizing the layers helps in data management and organization, ensuring pertinent layers are used, updated, or shared.

This article describes the workflow to list the selected layers from an ArcGIS Pro map using ArcPy.

Procedure

Note:
This workflow requires a full script to run in the ArcGIS Pro Python window. The indents must be retained as portrayed in the code block.
  1. In ArcGIS Pro, open the map containing the layers.
  2. Open the Python window. Refer to ArcGIS Pro: Python window for more information.
Note: 
Select the features interactively before running the Python code. Refer to ArcGIS Pro: Select features for instructions.
  1. Run the following script.
    1. Retrieve the current active map.
import arcpy

def get_selected_layers():
    active_map = arcpy.mp.ArcGISProject("CURRENT").activeMap
    1. Retrieve the list of layers in the map.
    layers = active_map.listLayers()
    1. Create an empty list to store the selected layers.
    selected_layers = []
    1. Loop through each layer to check if it is selected.
    for layer in layers:
        selection_set = layer.getSelectionSet()
        if selection_set:
            selected_layers.append(layer.name)

    return selected_layers
    1. Print the list of the selected layers.
if __name__ == "__main__":
    selected_layers = get_selected_layers()
    print("Selected Layers:")
    for layer_name in selected_layers:
        print(layer_name)

The code below demonstrates the full working script.

import arcpy

def get_selected_layers():
    active_map = arcpy.mp.ArcGISProject("CURRENT").activeMap

    layers = active_map.listLayers()

    selected_layers = []

  for layer in layers:
        selection_set = layer.getSelectionSet()
        if selection_set:
            selected_layers.append(layer.name)

    return selected_layers

if __name__ == "__main__":
    selected_layers = get_selected_layers()
    print("Selected Layers:")
    for layer_name in selected_layers:
        print(layer_name)

The image below shows the list of the selected layers printed in the Python window.

The list of the selected layers are printed in the Python window

Article ID: 000032814

Software:
  • ArcGIS Pro 3 1
  • ArcGIS Pro 3 3
  • ArcGIS Pro 3 2

Receive notifications and find solutions for new or common issues

Get summarized answers and video solutions from our new AI chatbot.

Download the Esri Support App

Related Information

Discover more on this topic

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options