When trying to create a legend on ArcMap use the following Python/ArcPy code I received an error message:

import arcpy
mxd = arcpy.mapping.MapDocument(r"E:\cities\MyProject.mxd")
df = arcpy.mapping.ListDataFrames(mxd)[0]
lyr1 = arcpy.mapping.Layer(r"E:\cities\cities.shp")
lyr2 = arcpy.mapping.Layer(r"E:\cities\region.shp")
lyr3 = arcpy.mapping.Layer(r"E:\cities\lakes.shp")
lyr4 = arcpy.mapping.Layer(r"E:\cities\rivers.shp")
legend = arcpy.mapping.ListLayoutElements(mxd,"LEGEND_ELEMENT")[0]
legend.autoAdd = True
arcpy.mapping.AddLayer(df, lyr1, "BOTTOM")
arcpy.mapping.AddLayer(df, lyr2, "BOTTOM")
arcpy.mapping.AddLayer(df, lyr3, "BOTTOM")
arcpy.mapping.AddLayer(df, lyr4, "BOTTOM")

and I get this message :

Traceback (most recent call last):
  File "E:\cities\scripts\Lgend.py", line 8, in <module>
    legend = arcpy.mapping.ListLayoutElements(mxd,"LEGEND_ELEMENT")[0]
IndexError: list index out of range

What does this indicate?

  • 2
    Do you have a legend in your document? – Nathan W Dec 12 '13 at 0:24
  • -1 down vote accept no i haven't a legend in the map document ,i want to add it but it dosn't work ,somthing is wrong in the code!! – user24541 Dec 12 '13 at 0:40

I think are misreading the API docs (if you have read them):

Returns a Python list of layout elements that exist within a map document (.mxd) layout.

This means if it doesn't exist you can't use this method.

legend = arcpy.mapping.ListLayoutElements(mxd,"LEGEND_ELEMENT") is returning a empty list so doing [0] will give you a index error.

  • sorry but i don't understand what do u mean,can u explain please ? – user24541 Dec 12 '13 at 0:48
  • It will only give you back what is already in the map document, if it's not there you can't get it back. If you want to create a new one you need to use a different method. – Nathan W Dec 12 '13 at 0:49
  • like what? can u help me please ?i'm a beginner in python . – user24541 Dec 12 '13 at 0:51
  • @user24541 You need to use ArcMap to insert a legend in your layout before arcpy.mapping can access it. It is not possible to create a legend element using ArcPy. – PolyGeo Dec 12 '13 at 2:35

It's not obvious to a python beginner but code like foo.method()[0] is calling foo.method and then it returns a list and [0] is indexing into the list. It is shorthand for this

  legendlist = arcpy.mapping.ListLayoutElements(mxd, "LEGEND_LIST")
  legend = legendlist[0]

If there are no elements of the type LEGEND_ELEMENT then the method ListLayoutElements returns an empty list, so indexing the empty list with [0] fails. The suggested method for python is to use a "try" block so the script would continue even without a legend.

  legend = arcpy.mapping.ListLayoutElements(mxd,"LEGEND_ELEMENT")[0]
  legend.autoAdd = True
  legend.adjustColumnCount = 2
except IndexError:
  print("This map does not have any legends.")

# add your layers here

You could also use an "if" statement to test the returned list (for example "if len(legendlist)...") but all the python "best practices" docs indicate a try block is preferred.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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