Technical skill practice
Python List: Create, Change and Copy a Packing List
A Python list is an ordered, mutable sequence. You can create one with square brackets, read items by position and replace items after creation. The important first question is ownership: assigning a list to another name gives both names the same list, while a shallow copy creates a new outer list.
Start with a small, explicit task
Build a packing list for two people. First let both names refer to the same flat list, then give one person a copy. Finally use a nested packing list to see whether copying the outer container also copies the inner one. Predict which original entries will change before running the checks.
Run the complete example
This original example was checked with Python 3.12.14. Save it as python_list.py and run it with Python 3.12. It uses only the standard library. The assertions check the stated results.
Expected output
['compass', 'bottle']
['compass', 'flask']
[['fruit'], ['jacket']]
shared = ["map", "bottle"]
partner = shared
partner[0] = "compass"
independent = shared.copy()
independent[1] = "flask"
nested = [["snack"], ["jacket"]]
shallow = nested.copy()
shallow[0][0] = "fruit"
assert partner is shared
assert independent is not shared
assert shared == ["compass", "bottle"]
assert independent == ["compass", "flask"]
assert nested == [["fruit"], ["jacket"]]
print(shared)
print(independent)
print(nested)
Explain the result
The first replacement is visible through shared because partner is another name for that same object. The independent list has its own outer slots, so changing its second slot does not replace the corresponding slot in shared. Equality compares contents; is compares identity, which is why the example checks both concepts.
The nested result is different: shallow and nested contain references to the same inner lists. Replacing an item inside the first inner list therefore affects both views. A shallow copy is useful when that sharing is intended. If your model requires independent nested objects, define and check that deeper copying requirement explicitly.
Avoid these mistakes
Do not treat a copy as a universal fix for every shared-state design. Decide which objects should remain shared. Keep list-method questions separate too: adding an item, removing a value and removing an index are different tasks, covered in the related tutorials.
Questions, answered
Does assigning a list copy it?
No. The two names initially refer to the same object, as the identity assertion demonstrates.
Does list.copy copy nested lists?
It copies the outer list. The nested example shows that the contained lists remain shared.
Sources and examples
ResumizeAI team. Examples are fictional teaching material, not customer results. Replace sample details with your own facts.
Your experience. A complete resume.
Build in Resumize with ATS-friendly templates, guided editing and a live preview. Start free, then upgrade when you need more.
Free account required. Free-plan PDF exports include a watermark.