1

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")
legend.adjustColumnCount(2)
mxd.save()

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?

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Dec 12, 2013 at 0:22
2
  • 2
    Do you have a legend in your document? Commented Dec 12, 2013 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!! Commented Dec 12, 2013 at 0:40

2 Answers 2

2

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.

answered Dec 12, 2013 at 0:40
4
  • sorry but i don't understand what do u mean,can u explain please ? Commented Dec 12, 2013 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. Commented Dec 12, 2013 at 0:49
  • like what? can u help me please ?i'm a beginner in python . Commented Dec 12, 2013 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. Commented Dec 12, 2013 at 2:35
1

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.

try:
 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.

answered Jul 31, 2017 at 22:20

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.