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)
  1. {'A': 'Kiran', 'B': 'Jay'}
  2. {'A': 'Isha', 'B': 'Jay'}
  3. {'A': ['Isha', 'Kiran'], 'B': ['Jay']}
  4. {'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

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