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)
- {'x': 1, 'y': 2}
- {'y': 2, 'x': 3}
- SyntaxError
- {'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
- A stationery shop updates its stock dictionary. What is printed?
- get() is called for a key that is not in the dictionary, with and without a…
- Which expression creates an empty set?
- A price is looked up with square brackets for an item that is not on the menu…
- The union and the intersection of two sets are measured. What does this print?
- From Python 3.7 onwards, in what order does a for loop go through the keys of…
- A key is added and an existing key is updated before the keys are listed. What…
- setdefault() is called twice with the same key. What does the dictionary look…
- A list is used as a dictionary key. What happens when this runs?
- Difference and symmetric difference of two sets are printed in sorted form…
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