Is this a bug in Python or something I do not understand.

Casey Caseyweb at gmail.com
Thu Jan 1 16:03:59 EST 2009


L1 is a list of three different lists, although each list holds the
same values.

L2 is a list of three references to the same list (the '*' operator
doesn't do a deep copy).  So when you modify any of the referenced
lists, you modify all of them.

Try this:

>>> q = [1, 1, 1]
>>> r = [q, q, q]
>>> r
[[1, 1, 1], [1, 1, 1], [1, 1, 1]]
>>> r[0][0] = 999
>>> r
[[999, 1, 1], [999, 1, 1], [999, 1, 1]]

Regards, Casey



More information about the Python-list mailing list