Dictionaries & Sets · Question 10 · Medium

A list is used as a dictionary key. What happens when this runs?

d = {}
d[[1, 2]] = "x"
print(d)
  1. TypeError
  2. KeyError
  3. {[1, 2]: 'x'}
  4. ValueError
Answer and explanation

Answer: A. TypeError

Explanation: Dictionary keys must be hashable, and a list is mutable and therefore unhashable. Python raises TypeError: unhashable type: 'list'. The tuple (1, 2) would work as a key.

Previous: setdefault() is called twice with the same key. What does the dictionary look… · Next: Difference and symmetric difference of two sets are printed in sorted form…

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