-1

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?

David
  • 2,926
  • 1
  • 27
  • 61

1 Answers1

4

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
iz_
  • 15,923
  • 3
  • 25
  • 40