<?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: Runtime error &amp;lt;type 'exceptions.IndexError'&amp;gt;: list index out of range in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/runtime-error-amp-lt-type-exceptions-indexerror/m-p/515157#M40415</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Are you running this within the Python Window? The 'current' keyword might be giving you trouble, I don't think you can call it from a script.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;mxd = arcpy.mapping.MapDocument("CURRENT")&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 05 Jun 2012 15:25:13 GMT</pubDate>
    <dc:creator>JohnCobb1</dc:creator>
    <dc:date>2012-06-05T15:25:13Z</dc:date>
    <item>
      <title>Runtime error &amp;lt;type 'exceptions.IndexError'&amp;gt;: list index out of range</title>
      <link>https://community.esri.com/t5/python-questions/runtime-error-amp-lt-type-exceptions-indexerror/m-p/515156#M40414</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;my List indices are 0 and 1 those are the only two.. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Can anyone help spot what's causing that?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;
import arcpy
from arcpy import env


arcpy.env.overwriteOutput = True

#set working MDB Here

arcpy.env.workspace = "C:/Users/atimpson/Desktop/Brunswick/PowerPlant.mdb/BPDMLayers"
dataList = arcpy.ListFeatureClasses()

mxd = arcpy.mapping.MapDocument("CURRENT")
lyrlist = arcpy.mapping.ListLayers(mxd)

# Reads a base table and outputs Feature Class and Layer Lists

readTable = open("C:/Users/atimpson/Desktop/ScriptTable/BPWorksLayers.csv")

# Figure out position of feature class and layer in the header
headerLine = readTable.readline()
valueList = headerLine.split(",")

fcPos = valueList.index("Feature Class")
lyrPos = valueList.index("Layers")

# Read lines in the file and append to comparison lists
baseFeature = []
baseLayer = []

for line in readTable.readlines():
 segmentedLine = line.split(",")
 baseFeature.append([segmentedLine[fcPos]])
 baseLayer.append([segmentedLine[lyrPos]])

#Performs comparisons between project data/layers and idealized data/layers

if "BPWorks 2 Layers" in lyr.name:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 

 for lyr, base in zip(lyrList, baseLayer):
&amp;nbsp; if lyr != base:
&amp;nbsp;&amp;nbsp; print(lyr, "is different from", base)
&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 05 Jun 2012 14:36:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/runtime-error-amp-lt-type-exceptions-indexerror/m-p/515156#M40414</guid>
      <dc:creator>AnthonyTimpson2</dc:creator>
      <dc:date>2012-06-05T14:36:21Z</dc:date>
    </item>
    <item>
      <title>Re: Runtime error &lt;type 'exceptions.IndexError'&gt;: list index out of range</title>
      <link>https://community.esri.com/t5/python-questions/runtime-error-amp-lt-type-exceptions-indexerror/m-p/515157#M40415</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Are you running this within the Python Window? The 'current' keyword might be giving you trouble, I don't think you can call it from a script.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;mxd = arcpy.mapping.MapDocument("CURRENT")&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 05 Jun 2012 15:25:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/runtime-error-amp-lt-type-exceptions-indexerror/m-p/515157#M40415</guid>
      <dc:creator>JohnCobb1</dc:creator>
      <dc:date>2012-06-05T15:25:13Z</dc:date>
    </item>
    <item>
      <title>Re: Runtime error &lt;type 'exceptions.IndexError'&gt;: list index out of range</title>
      <link>https://community.esri.com/t5/python-questions/runtime-error-amp-lt-type-exceptions-indexerror/m-p/515158#M40416</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Running from within Arcmap&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;i can individually call up the variables.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 05 Jun 2012 15:29:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/runtime-error-amp-lt-type-exceptions-indexerror/m-p/515158#M40416</guid>
      <dc:creator>AnthonyTimpson2</dc:creator>
      <dc:date>2012-06-05T15:29:24Z</dc:date>
    </item>
    <item>
      <title>Re: Runtime error &lt;type 'exceptions.IndexError'&gt;: list index out of range</title>
      <link>https://community.esri.com/t5/python-questions/runtime-error-amp-lt-type-exceptions-indexerror/m-p/515159#M40417</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Try testing the length of segmentedLine. Maybe it is not always what you expect.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 05 Jun 2012 16:21:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/runtime-error-amp-lt-type-exceptions-indexerror/m-p/515159#M40417</guid>
      <dc:creator>markdenil</dc:creator>
      <dc:date>2012-06-05T16:21:12Z</dc:date>
    </item>
    <item>
      <title>Re: Runtime error &lt;type 'exceptions.IndexError'&gt;: list index out of range</title>
      <link>https://community.esri.com/t5/python-questions/runtime-error-amp-lt-type-exceptions-indexerror/m-p/515160#M40418</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Not sure what's causing the problem exactly. But a couple of things could be cleaned up, and that might straighten out weird behavior. I most often get this same error when one of my lists unexpectedly contains zero items.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;First, I've always seen/used the mode type declared when creating a file object:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;readTable = open("C:/Users/atimpson/Desktop/ScriptTable/BPWorksLayers.csv", "r")&amp;nbsp; # 'r' is for 'read only'&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;Second, you've got some extra brackets, which means you're actually appending a list to a list. I'm guessing that's not what you intended to do:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;for line in readTable.readlines():
 segmentedLine = line.split(",")
 baseFeature.append(segmentedLine[fcPos])&amp;nbsp; # I removed an extra bracket here
 baseLayer.append(segmentedLine[lyrPos])&amp;nbsp; # and here&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;And finally, troubleshooting is a lot easier when you have line number and traceback information:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
# I wrap this around all of my scripts, pretty much.

import traceback
import sys

try:
&amp;nbsp;&amp;nbsp;&amp;nbsp; # all of your code here
except:
&amp;nbsp;&amp;nbsp;&amp;nbsp; tbinfo = traceback.format_tb(sys.exc_info()[2])
&amp;nbsp;&amp;nbsp;&amp;nbsp; print "Traceback Info:\n"
&amp;nbsp;&amp;nbsp;&amp;nbsp; for item in tbinfo:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print item + "\n"
&amp;nbsp;&amp;nbsp;&amp;nbsp; print "Error Info:\n{0}: {1}\n".format(sys.exc_type, sys.exc_value)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 22:30:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/runtime-error-amp-lt-type-exceptions-indexerror/m-p/515160#M40418</guid>
      <dc:creator>PhilMorefield</dc:creator>
      <dc:date>2021-12-11T22:30:31Z</dc:date>
    </item>
    <item>
      <title>Re: Runtime error &lt;type 'exceptions.IndexError'&gt;: list index out of range</title>
      <link>https://community.esri.com/t5/python-questions/runtime-error-amp-lt-type-exceptions-indexerror/m-p/515161#M40419</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Use Python csv module - it will make your life easier - see below &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import csv
# Reads a base table and outputs Feature Class and Layer Lists
readTable = csv.reader(open("C:/Users/atimpson/Desktop/ScriptTable/BPWorksLayers.csv", "rb")

# Read the first line
first_line = readTable.next()

## comment out these two lines
#headerLine = readTable.readline()
#valueList = headerLine.split(",")

fcPos = first_line.index("Feature Class")
lyrPos = first_line.index("Layers")

# Read lines in the file and append to comparison lists
baseFeature = []
baseLayer = []

# now read second line and on ..
for line in readTable:

&amp;nbsp;&amp;nbsp;&amp;nbsp; # make sure current line has as many items as the first line has
&amp;nbsp;&amp;nbsp;&amp;nbsp; if len(line) &amp;gt;= len(first_line):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; baseFeature.append(line[fcPos])
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; baseLayer.append(line[lyrPos])
&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 22:30:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/runtime-error-amp-lt-type-exceptions-indexerror/m-p/515161#M40419</guid>
      <dc:creator>NobbirAhmed</dc:creator>
      <dc:date>2021-12-11T22:30:33Z</dc:date>
    </item>
    <item>
      <title>Re: Runtime error &lt;type 'exceptions.IndexError'&gt;: list index out of range</title>
      <link>https://community.esri.com/t5/python-questions/runtime-error-amp-lt-type-exceptions-indexerror/m-p/515162#M40420</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;thank you everyone. I will try your suggestions this afternoon!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Really appreciate the help&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 05 Jun 2012 17:30:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/runtime-error-amp-lt-type-exceptions-indexerror/m-p/515162#M40420</guid>
      <dc:creator>AnthonyTimpson2</dc:creator>
      <dc:date>2012-06-05T17:30:37Z</dc:date>
    </item>
  </channel>
</rss>

