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

Dieter Maurer dieter at handshake.de
Tue Jun 15 04:32:02 EDT 2021


Chris Angelico wrote at 2021-6-15 05:35 +1000:
>On Tue, Jun 15, 2021 at 5:12 AM Jach Feng <jfong at ms4.hinet.net> wrote:
>>
>> >>> 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
>
>This is three tuples. Tuples are immutable and you get three
>references to the same thing.

In addition: object identity (as revealed by `id(...)`) is
an implementation detail. Do not rely on it!


More information about the Python-list mailing list