1

I am having a problem trying to assing a single value to a list within a list

example:

create a list with 11 lists of 11 strings board=[['.'](10+1)](10+1)

board [['.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.']]

assign value 'O' to the first string of the first list

board[0][0]='O'

I am getting to following output

board [['O', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.'], ['O', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.'], ['O', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.'], ['O', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.'], ['O', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.'], ['O', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.'], ['O', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.'], ['O', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.'], ['O', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.'], ['O', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.'], ['O', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.']]

Instead I was expected to have this other output that i am not getting:

[['O', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.'], ['.', '.', '.', '.', '.', '.', '.', '.', '.', '.', '.']]

That's it. I expected to get string 'O' only for the first element of the first listv in board!

I don't understand why i am getting value 'O' to the first value in every single list when I only selected the first one.

Thank you for any comment to all!

Rodriguez David
  • 541
  • 9
  • 25

2 Answers2

1

You can try this:

board = [["." for i in range(11)] for b in range(11)]

board[0][0] = "O"

That will give you the single "O" at the 0th index of the first list.

Ajax1234
  • 69,937
  • 8
  • 61
  • 102
1

In your code, this line:

>>> board = [['.']*(10+1)]*(10+1)

appears to create a 2D grid with independent rows of cells however it doesn't quite do that, and this is a common issue experienced with creating n-dimensional lists in Python.

What this code does is create one row and then the board is a list where each item in the list references the same row.

You can see that by using id():

>>> id(board[0])
4497981128

>>> id(board[1])
4497981128

>>> id(board[2])
4497981128

>>> id(board[0]) == id(board[1]) == id(board[2])
True

All three memory addresses are the same, so changing an element at index i in one, changes it in all of them.

What you want to do is generate each row separately:

>>> board = [['.']*(10+1) for _ in range(10+1)]

(The _ is a convention for a needed but unused variable.)

This way each row is a separate object in memory:

>>> id(board[0]) == id(board[1]) == id(board[2])
False

>>> id(board[0])
4497941384

>>> id(board[1])
4497989256

>>> id(board[2])
4497919688
Taylor D. Edmiston
  • 12,088
  • 6
  • 56
  • 76