Question 9
Back to Exercise
What is the output of the following Python program?
a = {"x": 1, "y": 2}
b = {"y": 20, "z": 30}
print(a | b, {**b, **a})
- A {'x': 1, 'y': 2, 'z': 30} {'y': 20, 'z': 30, 'x': 1}
- B {'x': 1, 'y': 20, 'z': 30} {'x': 1, 'y': 2, 'z': 30}
- C {'x': 1, 'y': 20, 'z': 30} {'y': 2, 'z': 30, 'x': 1} Correct
- D {'x': 1, 'y': [2, 20], 'z': 30} {'y': [20, 2], 'z': 30, 'x': 1}
Correct Answer: Option C
The right side wins for duplicate keys. Key order comes from where each key first appeared: b's keys first in the second merge.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!