This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author austin.green
Recipients austin.green
Date 2020-10-31.22:11:50
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1604182310.98.0.376819106818.issue42223@roundup.psfhosted.org>
In-reply-to
Content
Not sure if this is a bug, cannot find any clarification in the documentation.  Please reassign it if need be.

Using the * operator to generate a list of lists:
>>> a = [[0]*3]*3
>>> a
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
gives a list of 3 lists of zeroes, as expected.
But the data values appear to be shared, so e.g.
>>> a[1][1] = 'spam'
>>> a
[[0, 'spam', 0], [0, 'spam', 0], [0, 'spam', 0]]
Not what I was expecting!

A list comprehension gives the results I wanted:
>>> a = [[0 for x in range(3)] for x in range(3)]
>>> a
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
Looks just the same, but:
>>> a[1][1] = 'spam'
>>> a
[[0, 0, 0], [0, 'spam', 0], [0, 0, 0]]
gives a different result, and is actually what I was expecting from the first example.
History
Date User Action Args
2020-10-31 22:11:51austin.greensetrecipients: + austin.green
2020-10-31 22:11:50austin.greensetmessageid: <1604182310.98.0.376819106818.issue42223@roundup.psfhosted.org>
2020-10-31 22:11:50austin.greenlinkissue42223 messages
2020-10-31 22:11:50austin.greencreate