Add elements in a matrix in python
<p>I have this matrix:</p>

<pre><code>mat = [[ 0 for x in range(row)] for y in range(column)]
</code></pre>

<p>I tried to add elements to the matrix:</p>

<pre><code>for x in range(row): # row is 2 
    for y in range(column): # column is 3
        mat[x][y] = int(input("number: "))
</code></pre>

<p>but the shell returns this error:</p>

<pre><code>Traceback (most recent call last):
File "C:\Users\Fr\Desktop\pr.py", line 13, in &lt;module&gt;
mat[x][y] = 12
IndexError: list assignment index out of range
</code></pre>

<p>how do I add elements to a matrix?</p>

<p>The inner list should be based on columns:</p>

<pre><code>mat = [[ 0 for x in range(column)] for y in range(row)]
</code></pre>

<p>Here is an example:</p>

<pre><code>In [73]: row = 3
In [74]: column = 4
In [78]: mat = [[ 0 for x in range(column)] for y in range(row)]

In [79]: 

In [79]: for x in range(row): # row is 2 
             for y in range(column): # column is 3
                 mat[x][y] = 5
   ....:         

In [80]: mat
Out[80]: [[5, 5, 5, 5], [5, 5, 5, 5], [5, 5, 5, 5]]
</code></pre>

python python-3.x matrix