def get_next_state_for_x(list, next_state):
final = []
state = list
for g in range (len(next_state)):
temp = state[g]
state[g] = next_state[g]
print(state)
final.append(state)
state[g] = int(temp)
print(final)
get_next_state_for_x([0, 0, 0, 0], [1, 1, 1, 1])
so while i compile this code i get the output:
[1, 0, 0, 0]
[0, 1, 0, 0]
[0, 0, 1, 0]
[0, 0, 0, 1]
[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
instead of (for the last line)
[[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]
why does final.append(state) add wrong list to the result ?
question from:
https://stackoverflow.com/questions/65893446/why-doesnt-append-in-python-work-properly