Dictionaries & Sets · Question 20 · Hard
A grade dictionary is inverted with a comprehension. What is printed?
grades = {"Isha": "A", "Jay": "B", "Kiran": "A"}
inv = {g: n for n, g in grades.items()}
print(inv)
- {'A': 'Kiran', 'B': 'Jay'}
- {'A': 'Isha', 'B': 'Jay'}
- {'A': ['Isha', 'Kiran'], 'B': ['Jay']}
- {'B': 'Jay', 'A': 'Kiran'}
Answer and explanation
Answer: A. {'A': 'Kiran', 'B': 'Jay'}
Explanation: Two students share the grade A, and a key can appear only once, so the later name Kiran overwrites Isha. The key A keeps its first-inserted position, ahead of B.
Previous: A keys view is taken before the dictionary is changed. What does this print?
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
- Sets are built from the letters of two words. What is printed?
- New keys are added to a dictionary inside a for loop over the same dictionary…
- A keys view is taken before the dictionary is changed. What does this print?
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