0

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,032
  • 14
  • 39

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_
  • 14,740
  • 2
  • 25
  • 40