IndexError: list index out of range

1289
2
Jump to solution
11-24-2018 11:02 AM
DougWolfinger2
New Contributor II

I'm getting an IndexError saying it's out of range. But the Westerville dataframe is the first one and I'm using 0 to refer to it. After I run the program the cursor is at the end of the bolded line. What is Python trying to tell me? 

mxd = arcpy.mapping.MapDocument(r"E:\EsriTraining\ArcPy\MapScripting\Exercise3\Westerville.mxd")
# return a list of all data frames in the mapping doc and assign it to df
df = arcpy.mapping.ListDataFrames(mxd)[0]
print df.name
lyr = arcpy.mapping.ListLayers(mxd, " ", df)[1]

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Esteemed Contributor

You appear to be trying to listlayers that have a space as a name

lyr = arcpy.mapping.ListLayers(mxd, " ", df)[1]

View solution in original post

2 Replies
DanPatterson_Retired
MVP Esteemed Contributor

You appear to be trying to listlayers that have a space as a name

lyr = arcpy.mapping.ListLayers(mxd, " ", df)[1]

paulocress
New Contributor

An index in Python refers to a position within an ordered list . This error basically means you are trying to access a value at a List index which is out of bounds i.e greater than the last index of the list or less than the least index in the list. So the first element is 0, second is 1, so on. So if there are n elements in a list, the last element is n-1 . If you try to access the empty or None element by pointing available index of the list, then you will get the "List index out of range " error. To solve this error, you should make sure that you're not trying to access a non-existent item in a list.