Dictionaries & Sets · Question 3 · Easy

The same key is written twice in a dictionary literal. What is printed?

d = {"x": 1, "y": 2, "x": 3}
print(d)
  1. {'x': 1, 'y': 2}
  2. {'y': 2, 'x': 3}
  3. SyntaxError
  4. {'x': 3, 'y': 2}
Answer and explanation

Answer: D. {'x': 3, 'y': 2}

Explanation: Keys are unique, so the later value 3 overwrites the earlier value 1. The key keeps the position where it was first inserted, so x still comes before y.

Previous: get() is called for a key that is not in the dictionary, with and without a… · Next: Which expression creates an empty set?

Practise dictionaries & sets

Try a fresh set of dictionaries & sets questions with an explanation after every answer, free and without signing up: Dictionaries & Sets practice. Or sit a full placement mock test.

More dictionaries & sets questions

More python practice

Conditions & Loops · Functions & Scope · Comprehensions & Generators · Classes & Objects · Exceptions & Files · Built-ins & Standard Library · Variables, Types & Operators · Python Strings

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge