Inserting some debugging printhelps identify the problem:
p=[[3, 4], [2, 3]]
node=[5,3]
def foo(node, p_as_list):
print('p', id(p_as_list))
p_as_list=p_as_list[-1]
print('pm1', id(p_as_list))
p_as_list.append(node)
print('p', id(p))
print('pm1', id(p[-1]))
foo(node, p.copy())
print(p)
this prints:
p 1872370555144
pm1 1872370556232
p 1872370108872
pm1 1872370556232
[[3, 4], [2, 3, [5, 3]]]
Please note that idof has p[-1]not changed, you are working with the same list. This is because you create a shallow copy only when used list.copy()!
copy , :
p=[[3, 4], [2, 3]]
node=[5,3]
def foo(node, p_as_list):
p_as_list=p_as_list[-1].copy()
p_as_list.append(node)
foo(node, p)
print(p)