LearnFrenzy
Sign in Take a free test

Dictionaries · Predict the Output · Question 9

Merging dictionaries Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 9 Back to Exercise

What is the output of the following Python program?

main.py
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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links