Explanation:
1️⃣ Creating the list
clcoding = [[1, 2], [3, 4]]
A nested list (list of lists) is created.
Memory:
clcoding → [ [1,2], [3,4] ]
2️⃣ Copying the list
new = clcoding.copy()
This creates a shallow copy.
Important:
Outer list is copied
Inner lists are NOT copied (same reference)
๐ So:
clcoding[0] → same object as new[0]
clcoding[1] → same object as new[1]
3️⃣ Modifying the copied list
new[0][0] = 99
You are modifying inner list
Since inner lists are shared → original also changes
๐ Now both become:
[ [99, 2], [3, 4] ]
4️⃣ Printing original list
print(clcoding)
Because of shared reference, original is affected
๐ Output:
[[99, 2], [3, 4]]

0 Comments:
Post a Comment