Trace aliasing, shallow copy, and equality

Problem

Predict every component returned by alias_copy_report(). Explain which mutations reach b and c, and distinguish == from is.

Reveal answer or reference solution

b aliases the outer list, so it sees both mutations. c has a distinct outer list but shares the original inner lists, so it sees the 3 added inside the first inner list but not the new [4] appended to the outer list. a == b and a is b are true. a == c is false because their outer contents differ. c[0] is a[0] is true.

def alias_copy_report() -> tuple[object, ...]:
    a = [[1], [2]]
    b = a
    c = a.copy()
    a[0].append(3)
    a.append([4])
    return a, b, c, a == b, a is b, a == c, c[0] is a[0]

Public tests

  • alias_copy_report() → ([[1, 3], [2], [4]], [[1, 3], [2], [4]], [[1, 3], [2]], True, True, False, True)
Outcome

Previous attempts

Loading attempts saved in this browser…

    Review Screening Results