Predict the output
What happens?
d: dict[object, str] = {}
d[(1, 2)] = "ok"
d[[1, 2]] = "no"
print(d)Choose an answer, then open Show answer below to check it.
Show answer
Answer: A TypeError: unhashable type: 'list'
Right: lists are mutable, so Python refuses them as keys.
The tuple insert succeeds; the error comes from hashing the list, so the print never runs.
Why not the others?
- B {(1, 2): 'no'}. A list never compares equal to a tuple key, and it is refused before any comparison.
- C {(1, 2): 'ok', [1, 2]: 'no'}. That dict cannot exist; the second assignment raises.