Shallow Copy vs Reference Assignment

Medium ⏱ 12 min 47% acceptance ★★★★☆ 4.4
Write copy_behaviors(original) that demonstrates copying. Create ref by simple assignment (ref = original), and copy_a using slicing (original[:]), and copy_b using list(original). Then append the integer 999 only to original. Return a tuple (ref, copy_a, copy_b) reflecting their final states, showing that ref changed but the two copies did not.

Examples

Example 1
Input
original = [1, 2, 3]
Output
([1, 2, 3, 999], [1, 2, 3], [1, 2, 3])
Explanation

ref points to the same list object as original, so it reflects the append; copy_a and copy_b are independent lists.

Constraints

  • 0 <= len(original) <= 10^4

Topics

ListsCopying

Companies

MetaIBM

Hints

Hint 1

Assignment (=) does not create a new list — it creates another name for the same object.

Hint 2

Slicing [:] and list() both build a new, independent list.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.