0

My list looks like this:

board = [ [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0],
      [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0],
      [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0],
      [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0],
      [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0],
      [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0],
      [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0],
      [ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0] ]

How can I modify some of the elements? E.g.: the first element in each row = 0. I want to replace them with fives. So my list would look like this:

board = [ [ 5 , 0 , 0 , 0 , 0 , 0 , 0 , 0],
      [ 5 , 0 , 0 , 0 , 0 , 0 , 0 , 0],
      [ 5 , 0 , 0 , 0 , 0 , 0 , 0 , 0],
      [ 5 , 0 , 0 , 0 , 0 , 0 , 0 , 0],
      [ 5 , 0 , 0 , 0 , 0 , 0 , 0 , 0],
      [ 5 , 0 , 0 , 0 , 0 , 0 , 0 , 0],
      [ 5 , 0 , 0 , 0 , 0 , 0 , 0 , 0],
      [ 5 , 0 , 0 , 0 , 0 , 0 , 0 , 0] ]

1 Answer 1

1

You can access a specific cell of a 2D array by doing board[r][c], where r is the row and c is the column. For replacing the first column with 5's, you could do:

for row in board:
    row[0] = 5

This iterates over every row in board, and sets the first item in each row to 0.

Sign up to request clarification or add additional context in comments.

2 Comments

And if I get 2 numbers by input and only want to change 1 element? r=int(input("row: ")) c=int(input("column: ")) E.g. r=2 and c=6. So I want board[2][6] to be changed.
If you want one specific cell to be changed, you can do board[2][6] = newValue. Replace newValue with the value you want to set there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.