I have a python list that I want to append a list to. The list was declared like this:
data = []
Then I append the list with:
[0, 0, 0, 0, 0, 0, 0, 1, 0]
After that I want to append another list:
[0, 0, 0, 0, 0, -1, 0, 1, 0]
Then when I print out the entire list it shows:
[[0, 0, 0, 0, 0, -1, 0, 1, 0], [0, 0, 0, 0, 0, -1, 0, 1, 0]]
When I am trying to have it print out:
[[0, 0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, -1, 0, 1, 0]]
Where it updated the first element of the array when I just appended it like this:
data.append(prev_board)
And prev_board stores the list to append to the list. My entire code for this is:
def gather_training_data(games):
global data
global game_won
for each_game in range(games):
game = Game()
while True:
pygame.event.get()
game.create_board()
if not game_won:
prev_board = game.board
move = game.random_move()
data.append(prev_board)
print data
time.sleep(5)
else:
break
game_won = False
time.sleep(1)
window.fill((255, 255, 255))
return data
So why are the first elements of the list updating when I am only appending a list to the list?
You must be using the same list over and mutating it - something like this
>>> a = [1,2,3,4,5]
>>> b = []
>>> b.append(a)
>>> b
[[1, 2, 3, 4, 5]]
Now change the contents of a
>>> a[:4] = [9,9,9,9]
>>> a
[9, 9, 9, 9, 5]
>>> b
[[9, 9, 9, 9, 5]]
>>> b.append(a)
>>> b
[[9, 9, 9, 9, 5], [9, 9, 9, 9, 5]]
Now b
contains a
twice
>>> id(b[0]), id(b[1])
(50336328, 50336328)
>>> b[0] is b[1]
True
>>> id(a)
50336328
>>> a is b[0] is b[1]
True
>>>
To prevent this from happening, create new lists to append instead of mutating the same list or append copies of the mutated list if it cannot be helped.
copy — Shallow and deep copy operations
How to clone or copy a list?