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]])

Local history

Loading attempts saved in this browser…

Use with your agent

Share this URL and your attempt. Ask the agent to start with a clarifying question or the smallest useful hint.

Tutor me on https://mlprep.iwase.dev/programming/diagnostic/original-py-alias-copy/. If window.mlPrepAgent is available, read attempts for item original-py-alias-copy before tutoring. Inspect my attempt, keep the item ID, and do not reveal the full answer first. After a real attempt, append its record and read it back.

Appears in