Complete worked example
python
def add_item(values, item):
result = values.copy()
result.append(item)
return result- Reasoning
- The returned outer list is distinct while nested members remain shallowly shared.
Typorithm · easy
Step 1 of 4 · intro card
Return a changed outer list without accidentally modifying the caller's list.
A list is a mutable ordered collection. Assigning another name to a list does not copy its elements into a new list; both names can refer to the same outer object. The worked function copies the supplied outer list before appending one new item.
Its contract is explicit: preserve the input list and return a new outer list with the added item at the end. Repeated values are allowed in this lesson. Deduplication is a separate policy introduced later rather than an automatic property of every list operation.
list.copy creates a shallow outer copy.
list.copy creates a shallow outer copy. append then modifies that new outer list and returns None, so the function returns the list itself afterward. Returning values.append(item) would return None instead of the changed list the caller expects.
Shallow copying does not recursively copy nested objects. If the original list contains a nested list, the corresponding inner object remains shared with the returned result. The lab checks both outer independence and this declared inner aliasing rather than calling the result a deep snapshot.
python
def add_item(values, item):
result = values.copy()
result.append(item)
return resultThe counterexample binds result directly to values and appends through that alias.
The counterexample binds result directly to values and appends through that alias. It can return the expected visible values while silently mutating the caller's input. A return-value-only test misses that side effect, so the verification compares the original argument before and after the call.
A second mistake assumes a shallow copy makes nested structures independent. Choose deepcopy only when the contract actually requires recursive copying and understand its costs and custom-object behavior. This small function needs outer-list independence, not a universal object-graph cloning mechanism.
python
def add_item(values, item):
result = values
result.append(item)
return resultChange the function to place the new item at the beginning while keeping the original outer list unchanged.
Change the function to place the new item at the beginning while keeping the original outer list unchanged. Predict the new order for a two-item input and for an empty list. Keep the same shallow-copy scope; the task does not require copying nested members.
You can copy the list and insert at index zero, or create a new list by concatenation. Choose the clear operation you can explain. Do not use an in-place operation on the caller's list simply because its final returned values look correct.
Step 1: Create a new outer list. Step 2: Insert the new member at index zero and return that list.
python
def add_item(values, item):
# Prepend to a new outer list without modifying values.
raise NotImplementedErrorThe solution copies the input and inserts the new value at position zero.
The solution copies the input and inserts the new value at position zero. It returns a distinct outer list, preserves the caller's outer sequence and intentionally retains references to pre-existing nested members. The special alias probe checks the correct nested position for both variants.
Acceptance verifies order, values and non-mutation separately. These properties matter when a caller stores an earlier view of a list or reuses it for another computation. The exercise does not claim that every appended object is immutable or safe from future changes.
python
def add_item(values, item):
result = values.copy()
result.insert(0, item)
return resultIn an interview, distinguish equality from identity and assignment from shallow and deep copy.
In an interview, distinguish equality from identity and assignment from shallow and deep copy. Give a small nested example and say exactly which references are shared. This is more useful than claiming every copy creates an independent structure at all depths.
The next lesson combines mappings and sets. It will use explicit keys for counts and a sorted presentation of unique values, preserving the distinction between collection semantics and a deterministic output order.
Optional stretch — after the core task works: Write a nested mutation example that distinguishes shallow copying from a deliberate deepcopy.
Computer practice: https://applydjinn.com/typorithm/python-lab-20261105.zip. Run verify.py with your selected Python interpreter, then complete an exercise and check it with --practice followed by the lesson id. The lab uses only the standard library and disposable local fixtures; it never connects to the production database or company feeds.
Return a changed outer list without accidentally modifying the caller's list.