[Tutor] altering a list of lists

Ricardo Aráoz ricaraoz at gmail.com
Mon Nov 26 00:54:37 CET 2007

```Christina Margaret Berens wrote:
> I am supposed to make a very simple gameboard where a person is to
> find the gold by moving up, down, left, or right.  I have most of the
> math worked out and am trying to place the person on the game board.
> We were to make a list of lists for the board, but to place the person
> or gold on the board, I have to change one of the spaces to a '+', but
> when I do it, it changes all the elements in that position in each
> list.
>
> # set up of board
> Board = [['- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
>  -']]
> width = ['|']
> for i in range(30):
>     for j in range(1):
>         width.append('  ')
> width.append('|')
>

width here is only ONE object

> for i in range(30):
>     Board.append(width)

Here all your lines are the same object (not copies).
It will work ok if you do :
for i in range(30):
Board.append(width[:])
here ^^^ you are assigning a COPY of width

Or you might check :

Board = [['- ' * 30 + '-']]
for line in xrange(30):
Board.append(['|']+[' ' for i in xrange(59)] + ['|'])
Board.append(['- ' * 30 + '-'])
Board[2][3] = '+'
for i in range(32):
print ''.join(Board[i])

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|                                                           |
|  +                                                        |
|                                                           |
|                                                           |
|                                                           |
|                                                           |
|                                                           |
|                                                           |
|                                                           |
|                                                           |
|                                                           |
|                                                           |
|                                                           |
|                                                           |
|                                                           |
|                                                           |
|                                                           |
|                                                           |
|                                                           |
|                                                           |
|                                                           |
|                                                           |
|                                                           |
|                                                           |
|                                                           |
|                                                           |
|                                                           |
|                                                           |
|                                                           |
|                                                           |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
```