I am pretty new to Python and coding in general.
I tried appending a sublist into a list. I was initially thrown off since it seems the list appended all the values I wanted, but the values were the final value of [2,1].
With the following code
next_state = [0, 0]
def create_a_path(current_state):
possible_moves = [(2, 0), (1, 0), (1, 1), (2, 0), (1, 0)]
state_list = []
for pm in possible_moves:
#next_state = [0, 0] # this solves the issue
next_state[0] = current_state[0] + pm[0]
next_state[1] = current_state[1] + pm[1]
print(pm)
print(next_state)
state_list.append(next_state)
return state_list
current_state = [1, 1]
print(create_a_path(current_state))
I would think that my sublist would be appended and the sublist would be updated once there is another loop, then append without overwriting the previous appended value.
The simple change of code lines (as commented) solved this issue, but I am confused why.
Could anyone explain to me why this is? Maybe I am missing something fundamental about append, loops, or more? Thank you in advance.