Aliasing and shallow copy

Problem

Implement alias_copy_state(). Construct a = [[1], [2]], let b = a, and let c = a.copy(). Append 3 to a[0], then append [4] to a. Return (a, b, c). Predict the result before running it.

Starter code

def alias_copy_state():
    pass
Reveal answer or reference solution
def alias_copy_state():
    a = [[1], [2]]
    b = a
    c = a.copy()
    a[0].append(3)
    a.append([4])
    return a, b, c

Public tests

  • alias_copy_state() → ([[1, 3], [2], [4]], [[1, 3], [2], [4]], [[1, 3], [2]])
Outcome

Previous attempts

Loading attempts saved in this browser…

    Appears in