Why the list creates in two different ways? Does it cause by the mutability of its elements? Where the Python document explains it?

Jach Feng jfong at ms4.hinet.net
Sun Jun 13 05:31:18 EDT 2021


>>> n = [(1,2) for i in range(3)]
>>> n
[(1, 2), (1, 2), (1, 2)]
>>> id(n[0]) == id(n[1])  == id(n[2])
True
>>> m = [[1,2] for i in range(3)]
>>> m
[[1, 2], [1, 2], [1, 2]]
>>> id(m[0]) == id(m[1])  == id(m[2])
False
>>>

--Jach


More information about the Python-list mailing list