I have the following code:
a = [1,2]
b = list(a)
print id(a[0])
print id(b[0])
list a and b have different memory locations. but the elements within them have the same location. how can i make these 2 different.
I have the following code:
a = [1,2]
b = list(a)
print id(a[0])
print id(b[0])
list a and b have different memory locations. but the elements within them have the same location. how can i make these 2 different.
list
creates a new list, but won't do a deep copy, i.e. clone all the elements inside.
What you need is a clone
(also known as a deep copy
). See this answer for good alternatives (and benchmarks) in python: How to clone or copy a list?.