<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: arcpy.mapping.ListLayoutElements List Index Out Of Range in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/arcpy-mapping-listlayoutelements-list-index-out-of/m-p/1131631#M63439</link>
    <description>&lt;P&gt;You can add conditionals to check for empty lists too before assigning an index.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;noteElements = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "noteTitle")
# One liner method
noteTitle = noteElements[0] if noteElements else ''

# -----
# Three liner method
noteTitle = ''
if noteElements: # check if not empty []
    noteTitle = noteElements[0]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 07 Jan 2022 16:04:54 GMT</pubDate>
    <dc:creator>Anonymous User</dc:creator>
    <dc:date>2022-01-07T16:04:54Z</dc:date>
    <item>
      <title>arcpy.mapping.ListLayoutElements List Index Out Of Range</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-mapping-listlayoutelements-list-index-out-of/m-p/1131623#M63435</link>
      <description>&lt;P&gt;I am taking over a script from someone who has since retired. This script is used to create pdf documents of quarter sections from a map series created by data driven pages (ArcGIS 10.7). On the pdf there are map notes (among others as you can see below) from a feature class called NoteFeatures. When I try to run this script I keep getting an List Index Out Of Range error. Im still somewhat of a novice at Python so I am not sure what I am missing.&lt;/P&gt;&lt;P&gt;import arcpy, os, sys&lt;/P&gt;&lt;P&gt;# Get the data driven pages object from your mxd&lt;BR /&gt;# You will use this to export your pages&lt;BR /&gt;mxd = arcpy.mapping.MapDocument(r"C:\Users\SShenbe1\Desktop\Project\VMPrinting4.mxd")&lt;BR /&gt;ddp = mxd.dataDrivenPages&lt;/P&gt;&lt;P&gt;# Get the layer you are using as an Index Layer&lt;BR /&gt;# In this case the layer is in the data frame "Layers" and called "TWGrid3"&lt;BR /&gt;df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]&lt;BR /&gt;gridLayer = arcpy.mapping.ListLayers(mxd, "TWGrid3", df)[0]&lt;/P&gt;&lt;P&gt;# Get the layer for the feature class used to indicate inset map locations&lt;BR /&gt;# This needs to be in the same data frame as your index layer&lt;BR /&gt;# In this case the layer is in the data frame "Layers" and called "DetCircles2"&lt;BR /&gt;# First, make a layer from the feature class "DetCircles2"&lt;BR /&gt;arcpy.env.workspace = "C:\Users\SShenbe1\Desktop\Project"&lt;BR /&gt;arcpy.MakeFeatureLayer_management("DetCircles2.shp", "Details_lyr")&lt;BR /&gt;detailCircles = "Details_lyr"&lt;/P&gt;&lt;P&gt;# Set the base values for the position of your first inset map&lt;BR /&gt;# Locations of all the inset maps will be calculated based on these values&lt;BR /&gt;baseXpos = 3.0&lt;BR /&gt;baseYpos = 12.9&lt;/P&gt;&lt;P&gt;# Create objects for the layout elements that will be moving, e.g., notes and abandonment elements&lt;/P&gt;&lt;P&gt;notes = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "NOTES")[0]&lt;BR /&gt;abandonments = arcpy.mapping.ListLayoutElements(mxd, "GRAPHIC_ELEMENT", "ABANDONMENTS")[0]&lt;BR /&gt;triangle = arcpy.mapping.ListLayoutElements(mxd, "GRAPHIC_ELEMENT", "TRIANGLE")[0]&lt;BR /&gt;diamond = arcpy.mapping.ListLayoutElements(mxd, "GRAPHIC_ELEMENT", "DIAMOND")[0]&lt;BR /&gt;noteTitle = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "noteTitle")[0]&lt;BR /&gt;abandonmentTitle = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "abandonmentTitle")[0]&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;</description>
      <pubDate>Fri, 07 Jan 2022 15:45:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-mapping-listlayoutelements-list-index-out-of/m-p/1131623#M63435</guid>
      <dc:creator>Scott_Shenberger</dc:creator>
      <dc:date>2022-01-07T15:45:46Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy.mapping.ListLayoutElements List Index Out Of Range</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-mapping-listlayoutelements-list-index-out-of/m-p/1131628#M63437</link>
      <description>&lt;P&gt;That means one of your lists is either empty, or the indexing ( the '[0]' ) is trying to reference an item that is more than what the list contains. So in one of these lines, the list is empty and the code cannot reference the index ([0])&lt;/P&gt;&lt;LI-CODE lang="python"&gt;df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
gridLayer = arcpy.mapping.ListLayers(mxd, "TWGrid3", df)[0]
notes = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "NOTES")[0]
abandonments = arcpy.mapping.ListLayoutElements(mxd, "GRAPHIC_ELEMENT", "ABANDONMENTS")[0]
triangle = arcpy.mapping.ListLayoutElements(mxd, "GRAPHIC_ELEMENT", "TRIANGLE")[0]
diamond = arcpy.mapping.ListLayoutElements(mxd, "GRAPHIC_ELEMENT", "DIAMOND")[0]
noteTitle = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "noteTitle")[0]
abandonmentTitle = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "abandonmentTitle")[0]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;think of it this way.&amp;nbsp; Python is a zero indexed language so a list of 5 items is indexed like:&lt;/P&gt;&lt;P&gt;alistof5Items = [0, 1, 2, 3, 4]&lt;/P&gt;&lt;P&gt;alistof5Items[5] will throw an index out of range error because it is trying to get the 6 item in the list, which there is none.&lt;/P&gt;&lt;P&gt;Step through your code with a debugger or add some print statements printing the length of the item to tell which list is empty.&amp;nbsp; Your error that you are getting now might even tell you which line is failing so check the error message in more detail.&lt;/P&gt;</description>
      <pubDate>Fri, 07 Jan 2022 15:56:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-mapping-listlayoutelements-list-index-out-of/m-p/1131628#M63437</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2022-01-07T15:56:27Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy.mapping.ListLayoutElements List Index Out Of Range</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-mapping-listlayoutelements-list-index-out-of/m-p/1131631#M63439</link>
      <description>&lt;P&gt;You can add conditionals to check for empty lists too before assigning an index.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;noteElements = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "noteTitle")
# One liner method
noteTitle = noteElements[0] if noteElements else ''

# -----
# Three liner method
noteTitle = ''
if noteElements: # check if not empty []
    noteTitle = noteElements[0]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Jan 2022 16:04:54 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-mapping-listlayoutelements-list-index-out-of/m-p/1131631#M63439</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2022-01-07T16:04:54Z</dc:date>
    </item>
  </channel>
</rss>

