[Tutor] altering a list of lists

John Fouhy john at fouhy.net
Sun Nov 25 23:02:21 CET 2007


On 26/11/2007, Christina Margaret Berens <bere6944 at blue.unco.edu> 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.
[...]

Here is something for you to try:

>>> x = [3]
>>> y = [x, x, x]
>>> print y
>>> y[1].append(2)
>>> print y
>>> print x

Does anything you see here surprise you?  Can you explain what is going on?

-- 
John.


More information about the Tutor mailing list