Analyze the following Python code snippet and identify the correct output: D1…
2026
Analyze the following Python code snippet and identify the correct output:
D1 = {1: 4, 5: 6, 3: 2, 1: 7}
D2 = D1
D3 = D1.copy()
D2.popitem()
D3[3] = 8
print(D1)
- A.
{1: 7, 5: 6, 3: 2}
- B.
{1: 4, 5: 6, 3: 2, 1: 7}
- C.
{1: 7, 5: 6}
- D.
This program will raise an Error
Attempted by 395 students.
Show answer & explanation
Correct answer: C
Start with dictionary:
D1 = {1: 4, 5: 6, 3: 2, 1: 7}
Duplicate keys are not allowed, so key 1 gets overwritten:
D1 = {1: 7, 5: 6, 3: 2}
D2 = D1 → both refer to the same dictionary
D3 = D1.copy() → separate copy
D2.popitem() removes the last inserted item from D1
Last inserted key is 3, so it is removed:
D1 = {1: 7, 5: 6}
D3[3] = 8 modifies only D3, not D1
Final output:
{1: 7, 5: 6}