Explanation:
๐น 1. Creating the List
x = [[1]*2]*2
๐ Break it step by step:
[1]*2 → creates [1, 1]
[[1]*2]*2 → creates two references to the SAME inner list
So it becomes:
x = [[1, 1], [1, 1]]
⚠️ Important:
Both inner lists are not separate — they point to the same memory object
๐น 2. Modifying an Element
x[1][1] = 9
๐ This means:
Go to 2nd row (x[1])
Change 2nd element ([1]) → set to 9
But since both rows are the same object, the change affects BOTH rows
๐น 3. Printing the List
print(x)
๐ Output becomes:
[[1, 9], [1, 9]]

0 Comments:
Post a Comment