Suppose the following code:
a = [1,2,3]
b = list(a)
print(id(b) == id(a))
This yields to:
False
I would have expected both lists to be of the same id after copying. Why doesn't b have the same id as a?
Suppose the following code:
a = [1,2,3]
b = list(a)
print(id(b) == id(a))
This yields to:
False
I would have expected both lists to be of the same id after copying. Why doesn't b have the same id as a?
list
creates a new copy of the argument. It expects an iterable and consumes all of the iterable's elements into a new list.
If you wanted a an additional reference/name that refers to the same list, simply use assignment without the call to list
.
a = [1,2,3]
b = a
print(id(b) == id(a)) # True