Runtime error <type 'exceptions.IndexError'>: list index out of range

868
3
Jump to solution
08-16-2012 01:57 PM
AngelaCunningham
New Contributor II
I am brand new to python coding, trying to figure out how to set up a script to allow me to apply the changes made to the .lyr's in one map to the .lyr's in other maps.

I've been working my way through the code samples found here: http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/UpdateLayer/00s30000003p000000/


Changing just the symbology works fine, but replacing a layer hasn't been.

here's the code, based on the ESRI example:
>>> mxd=arcpy.mapping.MapDocument(r"C:\Users\arc\Desktop\python_tutorial.mxd") >>> df=arcpy.mapping.ListDataFrames (mxd,"Layers")[0] >>> sourceLayer=arcpy.mapping.ListLayers(mxd,"orange",df)[0] >>> mxd2=arcpy.mapping.MapDocument(r"C:\Users\arc\Desktop\python_tutorial2.mxd") >>> df2=arcpy.mapping.ListDataFrames (mxd2,"Layers")[0] >>> updateLayer=arcpy.mapping.ListLayers(mxd2,"orange",df2)[0] Runtime error <type 'exceptions.IndexError'>: list index out of range


What exactly does that error message mean? I wondered if it had something to do with the names in my sources ("Layers", "orange") being the same as the names in my targets, but if I change the names I get the same error. I tried running it in Catalog, and still the same error. (I tried running it in commandline and through IDLE and found I had NO idea what I was doing.)

any help would be greatly appreciated.

cheers,
Angela
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MathewCoyle
Frequent Contributor
Are you sure you don't want to work with your current map document?

Try listing the layers to make sure you get what you expect.

layers = arcpy.mapping.ListLayers(mxd2,"",df2) for layer in layers:     print layer

View solution in original post

0 Kudos
3 Replies
MathewCoyle
Frequent Contributor
Are you sure you don't want to work with your current map document?

Try listing the layers to make sure you get what you expect.

layers = arcpy.mapping.ListLayers(mxd2,"",df2) for layer in layers:     print layer
0 Kudos
curtvprice
MVP Esteemed Contributor
What exactly does that error message mean?


I would guess that the error message means the the list from which you're trying to pull the first element ([0]) is empty. I'd check the arguments to your ListLayers function to make sure they are valid.

The way to know for sure is to put this debug statement right before it:

layers = arcpy.mapping.ListLayers(mxd2,"orange",df2)
print len(layers), str(layers)
0 Kudos
AngelaCunningham
New Contributor II
Thank you Matthew and Curtis,

I wasn't using "CURRENT" because I wanted to use two maps at once... which I guess isn't possible if you're operating from inside Arc? I figured out how to work in IDLE and using paths instead of "CURRENT" seems to work there.
0 Kudos