Python multidimensional array list index out of range
<p>I'm trying to make a program that will read a text file of names and numbers and make it into a 2d array with the names and numbers, then remove the item from the array if the number is 1. However, the code I wrote to access the index in the 2d array doesn't work and throws the error "IndexError: list index out of range". This is the block of code that isn't working:</p>

<pre><code>for i in range(x):
    list2[i][1] = int(list2[i][1])
    if int(list2[i][1]) == 1:
        list2.pop(i)
</code></pre>

<p>This is the traceback:</p>

<pre><code>File "/Users/cat/PycharmProjects/myCS106/Names.py", line 16, in updateNames
list2[i][1] = int(list2[i][1])
IndexError: list index out of range
</code></pre>

<p>This is an example of what list2 might look like:</p>

<pre><code>[["John Doe","1"],["Jane Smith","0"],["Firstname Lastname","1"]]
</code></pre>

<p>What am I doing wrong, and how do I correctly access the second part (in this case, the number) of the items in the array?</p>

<p>In case you just want to remove some elements from the list you could iterate it from end to beginning which would allow you to remove items without causing <code>IndexError</code>:</p>

<pre><code>list2 = [["John Doe","1"],["Jane Smith","0"],["Firstname Lastname","1"]]

for i in range(len(list2) - 1, -1, -1):
    list2[i][1] = int(list2[i][1])
    if int(list2[i][1]) == 1:
        list2.pop(i)

print(list2)
</code></pre>

<p>Output:</p>

<pre><code>[['Jane Smith', 0]]
</code></pre>

<p>Of course you could just use <a href="https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions" rel="nofollow">list comprehension</a> to create a new list:</p>

<pre><code>list2 = [[x[0], int(x[1])] for x in list2 if int(x[1]) != 1]
</code></pre>

python arrays multidimensional-array indexoutofrangeexception