A Python list stores an ordered mutable sequence of object references.
Python lists: slicing copies the outer sequence, not nested objects
Operation contract
The receipt batch contains nested lists. A slice copies the outer sequence, so appending another receipt to the copy leaves the outer original length unchanged. Editing an inner amount still changes the original because both outer lists refer to that same inner list. The program prints both states to expose the shared reference.
Failure and ownership boundary
Use a copy policy that matches the value model. A full recursive copy can duplicate more state than intended, while immutable receipt records can make a shallow outer snapshot sufficient. List append also mutates in place and returns None; assigning its return value discards the reference the caller expected. Python tuples: immutable containers can still contain mutable state and Python dataclasses: frozen fields require an immutable value model have related ownership limits.
Working program
receipt_batch = [[41, 125], [42, 75]]
snapshot = receipt_batch[:]
snapshot[0][1] = 200
snapshot.append([43, 50])
print(receipt_batch)
print(snapshot)
print(receipt_batch[0] is snapshot[0])Output
[[41, 200], [42, 75]]
[[41, 200], [42, 75], [43, 50]]
TrueCosts and limits
Slicing n entries takes O(n) time/storage for the outer references. Front deletion shifts remaining list references; a queue should consider deque instead of repeated pop(0).
Common Mistakes
- A slice is a shallow copy.
- Do not assign the return value of append as the new list.
Connected lessons
Python variables: names refer to objects, assignment does not copy, Python tuples: immutable containers can still contain mutable state, Python graph BFS: mark a vertex when it enters the queue, Java ArrayList and indexed access.
Apply this boundary
Python shallow and deep copies: preserve aliases deliberately, NumPy slices and indexed copies: verify who owns the buffer.
Related Python operation checks
Python segment tree: point replacement and half-open range sums, Python memoryview: shared buffers and explicit release.
