0

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.

Chuah Zi Yang
  • 29
  • 1
  • 4

1 Answers1

1

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?.

Community
  • 1
  • 1
Derlin
  • 9,572
  • 2
  • 32
  • 53