I am trying to constantly append a changing variable into a list but I realized that it only append the address so the outputs are all the same
one_vas = ["0", "0"]
canvas = []
for i in range(4):
canvas.append(one_vas)
for i in canvas:
print(id(i))
the output indicates that all the elements in canvas
has the same address so when I change one line it affects other lines as well
The output:
2100899664256
2100899664256
2100899664256
2100899664256
Is there any way to append a variable with a unique address into the list with different addresses. I already tried using append()
, insert()
, and +=
but it all came back with the same address
Huy Nguyễn is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.