I'm initializing a list and then updating values in it, however the variable I am using for initializing the rows is also getting updated retrospectively - so to speak.
I expected this code to output [['first', ''], ['', 'last']]
. How can I change my code so it outputs that?
array_row = ['', '']
array = []
# array = [['', '']]
array.append(array_row)
# array = [['first','']]
# array_row = ['first', ''] <---- !!! Why does array_row also get updated???
array[0][0] = 'first'
# array = [['first',''], ['first','']]
# array_row = ['first', '']
array.append(array_row)
# array = [['first','last'], ['first','last']]
# array_row = ['first', 'last'] <---- !!! Why does array_row also get updated???
array[1][1] = 'last'
# [['first', 'last'], ['first', 'last']]
print(array)