May I ask if there are any PRACTICAL differences if multiple immutable tuples share the same address or not?
I mean if I use a tuple in a set or as the key in a dictionary and a second one comes along, will it result in two entries one time and only one the other time? Some languages I use often have a lazy evaluation where things are not even copied when doing some things and only copied if absolutely needed as in when you make a change. So you can create say a data.frame then make another from it with some columns added and removed and the storage used does not change much, but add or remove one or two, and suddenly entire large amounts get copied. So by that argument, you could have the copies of the list also be the same at first and since they are mutable, they might diverge later or not but tuples would never change so why not share if the compiler or interpreter was set to use less space when possible. Now if you really still want true copies, what ways might fool a compiler? NoDup = [(5, 2), (6-1, 6/3), (12%7, 1/1 + 1/1)] -----Original Message----- From: Python-list <python-list-bounces+avigross=verizon....@python.org> On Behalf Of Greg Ewing Sent: Tuesday, June 15, 2021 7:11 PM To: python-list@python.org Subject: Re: Why the list creates in two different ways? Does it cause by the mutability of its elements? Where the Python document explains it? On 15/06/21 7:32 pm, Jach Feng wrote: > But usually the list creation is not in simple way:-) for example: >>>> a = [1,2] >>>> m = [a for i in range(3)] >>>> m > [[1, 2], [1, 2], [1, 2]] >>>> id(m[0]) == id(m[1]) == id(m[2]) > True The first line is only executed once, so you just get one list object [1, 2]. You then refer to that object three times when you build the outer list. To get three different [1, 2] lists you would need something like this: m = [[1,2] for i in range(3)] This executes the [1, 2] expression 3 times. Because lists are mutable, you can be sure that this will give you 3 distinct list objects. -- Greg -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list