&amp;amp;quot;IndexError: list index out of range&amp;amp;quot; - when trying to Update Layer in ArcPy

Hi!I am trying to run (what I thought) was a very simple script that updates the symbology in a Layer File based on another layer file. Being very new to Python, I "lifted" some example code straight from the ArcGIS10 help ( http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/UpdateLayer/00s30000003p000000/ )The layer file to be updated in my map document is part of a grouped layer file within a grouped layer file.I keep getting an IndexError and I have no idea why!?! There is only one dataframe (Layers). Any help is greatly appreciated!Here is my code:import arcpymxd = arcpy.mapping.MapDocument(r"N:\Templates\v10\Base Maps\A_Portrait.mxd")df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]updateLayer = arcpy.mapping.ListLayers(mxd, "Base Map Layers\Bay, Lakes & Streams", df)[0]sourceLayer = arcpy.mapping.Layer(r"N:\Users\ke\Bay, Lakes & Streams.lyr")arcpy.mapping.UpdateLayer(df, updateLayer, sourceLayer, True)mxd.save()del mxd, sourceLayerand this is the error message that I get:====>>> Traceback (most recent call last):File "N:\Users\ke\ArcPyMapping\UpdateLayerFile.py", line 4, in <module>updateLayer = arcpy.mapping.ListLayers(mxd, "Base Map Layers\Bay, Lakes & Streams", df)[0]IndexError: list index out of range

Since you are specifying exctly what layer you are interested in ("Base Map Layers\Bay, Lakes & Streams"), try leaving the [0] index off. The [0] being a refrence to the 1st item in a list, but this time you are only expecting one item anyway...

Thanks for the reply Chris.Unfortunatly none of that seemed to work. When I added \\ or r"\", the same message appeared.However when I removed the [0] from line four, I got a totally new error message (which is even more confusing!):Traceback (most recent call last): File "N:\Users\ke\ArcPyMapping\UpdateLayerFile.py", line 6, in <module> arcpy.mapping.UpdateLayer(df, updateLayer, sourceLayer, True) File "C:\Program Files\ArcGIS\Desktop10.0\arcpy\arcpy\utils.py", line 181, in fn_ return fn(*args, **kw) File "C:\Program Files\ArcGIS\Desktop10.0\arcpy\arcpy\mapping.py", line 1458, in UpdateLayer assert isinstance(update_layer, Layer)AssertionError

Your first and second error messages seem consistent to me: your code dies the first time on the arcpy.mapping.ListLayers call, which returns a list of layers that suit the filter, or an empty list '[]' if no results are found. I think your filter is returning an empty list (no values found), and so the function is trying to return the first element of your empty list, which kills it with an IndexError.

[][0] = DEAD

Then, by removing the [0], the function successfully passes on the empty list to the arcpy.mapping.UpdateLayer, which dies because it's being handed an empty list to work on.

arcpy.mapping.UpdateLayer(df, [], sourceLayer, True) = DEAD

I would recommend calling arcpy.mapping.ListLayers with the mxd and no filter, to see what name is being assigned to your layer. The List methods in arcpy are relatively fickle and need an exact match of the value being searched for, or at lest wildcards around them.

For example: shapethingy.shp will not appear under ListFeatureClasses(filter) if filter = 'shapethingy', but but will if filter = 'shapethingy*' or 'shapethingy.shp'

In fact, if this is your only layer, you don't even need to add in a filter: just call the layer directly by name in your other functions (since the only thing being passed around by ListLayers is a string, not an object)

Well, I finally got it to work, and the trick was putting it into a "for" loop. I still really don't understand the whole index thing, and I'm very new to Python, but this was the only way I could force it to find "Bay, Lakes & Streams"When I take the [0] off, it would still fail.Here is the code that works:import arcpymxd = arcpy.mapping.MapDocument(r"N:\Templates\v10\Neighborhood Templates\York_NhoodMap.mxd")df = arcpy.mapping.ListDataFrames(mxd)[0]updateLayer = arcpy.mapping.ListLayers(mxd, r"Bay, Lakes & Streams", df)[0]sourceLayer = arcpy.mapping.Layer(r"N:\Users\ke\BayLake.lyr")for lyr in arcpy.mapping.ListLayers(mxd, "Bay, Lakes & Streams", df): arcpy.mapping.UpdateLayer(df, updateLayer, sourceLayer, False) mxd.save()del mxd, sourceLayerThanks for everyones tips and tricks : )

Jeff-So, being new to Python... What is the difference between a Layer Object and Python List Object.I'm not seeing anything different in your two lines that you posted that would differentiate between the two?

Jeff (while I got ya here ; )If I used the index of [0], isn't this just calling the first returned object in the list? My "Bays, Lakes & Streams" grouped layer file is way at the bottom of my table of contents...which I would think would mean that instead of [0] I would be calling [21], which when I ran the "Longname" command and counted it's place, that is where it was...Thanks for your help. I did attend a conference where you were doing hands-on ArcPy.mapping workshop almost a year ago in Tacoma, unfortunately we only just finally got 10 installed so I'm just now trying to dive into using ArcPy.mapping.

This returns a Python list object. If you want to get to items in the list you need to either use and index number or a for loop. If your MXD is designed with all layers having unique names, then if you use the correct filter, you will only have one item in your list and you can use [0] to extract it.

There is a new arcpy.mapping tutorial that covers this in much better detail.