Python: list index out of range (Assigning a 2D list to another 2D list)
<p>I am processing a 2D list and want to store the processed 2D list in a new 2D list with same indexes. But, I am getting error "list index out of range"</p>

<p>I have simplified my code to make it easy to understand:</p>

<pre><code>a = [
    ['a', 'b'],
    ['c', 'd']
]
print a

b = []
for i in range(len(a)):
    for j in range(len(a[i])):
        if a[0][0] == 'a':
            a[0][0] = 'b'
        else:
            b[i][j] = a[i][j] #list index out of range
</code></pre>

<p>Thanks</p>

<p>Simple, you initialised b to [], so any attempt to access it by index will be out of range. Lists in Python can increase in size dynamically, but this isn't how you do it. You need .append</p>

python arrays list