Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to reference a Layer named "Tim" in the dataframe "Layers" in my .mxd. It is not working. I have looked at the ArcGIS online help and all else in this forum. Can't get rid of this Runtime Error!

>>> mxd = arcpy.mapping.MapDocument("CURRENT")
>>> df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
>>> layer = arcpy.mapping.ListLayers(mxd,"Tim",df)[0]
Runtime error <type 'exceptions.IndexError'>: list index out of range

Any insight would be greatly appreciated!!

So this is my entire script:

import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "layers")[0]
targetGroupLayer = arcpy.mapping.ListLayers(mxd, "Tim", df)[0]
addLayer = arcpy.mapping.Layer(r"MyLayer.lyr")
arcpy.mapping.AddLayerToGroup(df, targetGroupLayer, addLayer, "BOTTOM")

this also give me the error.

When i change

targetGroupLayer = arcpy.mapping.ListLayers(mxd, "Tim", df)[0]

to this

targetGroupLayer = arcpy.mapping.ListLayers(mxd, "", df)[0]

it works but i need it do reference a specific group layer because there will be many of them and eventually i will pass in a variable for the targetGroupLayer so i can bring in certain layers into that group.

My most recent as Roy suggested.

import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
for lyr in arcpy.mapping.ListLayers(mxd, "",df):
    if == "Tim":
        addLayer = arcpy.mapping.Layer(r"C:\Users\T\Desktop\MyLayer.lyr")
        arcpy.mapping.AddLayerToGroup(df, lyr, addLayer, "BOTTOM")

No luck still!

Thanks for all the suggestion also!! I will keep trying!

** This script is working now!!* Thanks Roy!

share|improve this question
The layer Tim is a group layer if that matters. – Tim Jun 8 '12 at 15:52
is it work with not limiting result? layer = arcpy.mapping.ListLayers(mxd,"",df)[0] – Aragon Jun 8 '12 at 15:55
Ok just kidding now for whatever reason that latest script i poseted worked!! Thanks Roy! – Tim Jun 8 '12 at 20:39
up vote 4 down vote accepted

I've had luck with listing all the layers within a dataframe and checking with a loop as opposed to specifying dirctly in the ListLayers method. Same goes for the data frame.

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
for lyr in arcpy.mapping.ListLayers(mxd, "", df):
    if == "Tim":
        # Do some stuff.

To address your reference to it being in a group, from ESRI Help files:

Group layers are treated just like layers. The index values are simply generated from top to bottom as they appear in the table of contents or the way they would appear in a layer file. The same applies if a group layer is within another group layer. A map document with a single group layer with three layers within it will return a Python list of four layer objects, the group layer being the first. One way of determining if a layer is inside a group layer is to interrogate the longName property. A layer's longName will include the group layer name as part of the name.

share|improve this answer

I am sorry but the answer above is not a good option. Unless you need to perform the same operation on several layers. If you are interested in accessing one layer, it is better to know which object that is in the collection and reference it by its index.

layers = mapping.ListLayers(mxd)

layer = layers[0]
share|improve this answer
Dear John, maybe you could link to the link to the other answer. As answers are resorted based on votes, a reader wont be able to identify the other answer you are referring to. Also, you could emphasize the code lines by either using the backquotes ` (to wrap short text of code) or use the appropriate button (curly brackets) provided for code blocks when it comes to larger pieces of code. – Nikos Alexandris Dec 16 '13 at 21:28

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.