Reshma NirmalKumar - 1 year ago 60

Python Question

I need to update matrix values with elements in list. When i use for loop to iterate the index of matrix and include the elements in list. only the last value is updated as it is the last value of the iterator.

But i need the sequence of the list to be added in the matrix.

please let me know if there could be any possibilities to do this..

`>>> n`

[0, 1, 2, 3]

>>> for i in range(len(m)):

for j in range(len(m)):

for k in range(len(n)):

m[i][j]=n[k]

>>> m

array([[ 3., 3.],

[ 3., 3.]])

Answer Source

In the most inside loop:

```
for k in range(len(n)):
m[i][j]=n[k]
```

`i`

and `j`

doesn't changes, so you assign `n[i][j]=n[0]`

and then `n[i][j]=n[1]`

, `n[i][j]=n[2]`

, `n[i][j]=n[3]`

and just the last one holds.

A naive way to solve it is:

```
k=0
for i in range(len(m)):
for j in range(len(m)):
m[i][j]=n[k]
k+=1
```