Back to questions
JuniorTheoryCommon
What happens when this program runs?
names = {}
point = [1, 2]
print("start")
names[point] = "A"
print(names)Prints start, then a TypeError
- Expecting
{[1, 2]: 'A'}: a dict key must be hashable, and alistcan change, so it is not. The line raisesTypeError: unhashable type: 'list'. - Expecting nothing to print: the
TypeErrorhappens while the program runs, sostartprints first and the program stops atnames[point] = "A".
- How would you change
pointso that it works as a key? - Can a tuple that holds a list be a dict key?