I generate a list and each element in the list is a list too. I do it using a function with a 'for' loop in it. When I update the single value of the list it's updated in each sub-list instead.
I've noticed that when I create a list manually like:
array = [['O', 'O', 'O'], ['O', 'O', 'O'], ['O', 'O', 'O']]
all works as expected. But when I use my function which produces exactly the same output it fails.
def array_generation(size):
array = []
row = ['O'] * size
for i in range(size):
array.append(row)
return array
myArray = array_generation(3)
myArray[0][0] = "S"
Actual Result: [['S', 'O', 'O'], ['S', 'O', 'O'], ['S', 'O', 'O']]
Expected Result: [['S', 'O', 'O'], ['O', 'O', 'O'], ['O', 'O', 'O']]