Aug-18-2021, 07:26 PM
(Aug-18-2021, 03:35 PM)naughtyCat Wrote: the inner list [[]*3] produces a list of three lists. But then this list is duplicated three times. However, in python, it's really a list of references that is being multiplied, so the reference is duplicated, but each reference still points to the same underlying list.
solve:
A = [[' ' for _ in range(3)] for _ in range(3)] B = [[' ',' ',' '],[' ',' ',' '],[' ',' ',' ']] print(A == B) A[0][0] = 'X' B[0][0] = 'X' print(A) print(B)
Output:True [['X', ' ', ' '], [' ', ' ', ' '], [' ', ' ', ' ']] [['X', ' ', ' '], [' ', ' ', ' '], [' ', ' ', ' ']]
Thanks for the reply naughtyCat. I'm down to trying to understand when an assignment creates a new object vs references an existing object. Gotta do more reading on this subject. Perhaps immutable objects always get referenced and static things like a string get copied.