Practical technology learning
Python List Copies: Test Assignment, Shallow Copy and Nested Changes
A list that looks identical on screen is not necessarily independent. Use a nested collection to investigate what assignment and copying actually preserve. This small exercise uses invented integers and a fresh starting state for every run; it is about object behaviour rather than performance claims.
Prepare one controlled starting point
Use an outer list containing two inner lists, such as [[2, 4], [6, 8]]. Write a factory function or recreate that expression before each case. Label your notebook columns “operation”, “prediction”, “observed original” and “observed second name”.
Keep the numbers and mutation identical across comparisons. Changing the data, copying method and operation at the same time makes an unexpected result harder to explain. The goal is to identify one difference at a time.
Compare assignment with an outer-container copy
First bind a second name to the original list. Append a new inner list through that second name and inspect both names. Next reset the fixture, create a shallow copy with the list copy method, and repeat the outer append.
Assignment binds another name to an existing object. A shallow copy makes a new outer collection while retaining references to its contained objects. The append experiment reveals the outer-container distinction, but it does not yet establish independence at every depth.
Change a nested value in a separate run
Reset the fixture again. Make a shallow copy and change the first value inside its first inner list. Record both outer lists. Explain which inner object the two containers can still refer to, rather than describing the result only as a surprising printout.
Now import copy, create a deep copy of the fresh fixture and repeat the same nested edit. For this simple collection of lists and integers, compare the independent result with the shallow-copy observation. State that the example does not cover arbitrary custom objects.
Choose copying according to the intended contract
Write one requirement where shared nested values are intentional, and another where a caller needs independent editable nested values. Decide which experiment represents each requirement. Copying should follow the desired ownership of data rather than habit.
Deep copying can traverse more of an object structure and can duplicate objects that were intended to remain shared. Before applying it to a large application object, read the relevant type behaviour and consider a smaller, deliberate data structure.
Review your evidence
- Each case starts with newly created test data.
- Outer appends and inner edits are tested separately.
- The prediction is written before the mutation.
- The explanation identifies the shared level.
- The chosen copying strategy matches an explicit requirement.
Bring the experiment into your Python practice
Keep the script and observation table together so another learner can repeat the cases. Extend it with a dictionary containing a list only after the first model is clear. Mango Engineers’ Python course covers core collections; ask about the current practical sequence and prerequisites when discussing the learning path.