Search code examples
pythonlistmatrixinsert

Adding number at specific position in matrix


Please refer this link for code

It is not working, saying 'List index out of range' I am a beginner, is there correct use of the function or not? Is there any way to correct the solution or do we need to find another solution? Thank you!


Solution

  • If you just want to change an element use

    final_row[row - 1][column - 1] = your number here
    

    You can also check if the row already exists or if you have to create it

    if (len(final) < row) #Check if row exists
       final_row[row - 1][column - 1] = 0
    else:
       final_row.append([1,1,1]) #append a row
       final_row[row - 1][column - 1] = 0