← All tips

Python: shallow copy a list

pythonbasics

Use list(x) or slicing to avoid aliasing.

a = [1,2]
b = a[:] 
b.append(3)
print(a, b)