Explanation:
๐น 1. Creating an Empty Dictionary
d = {}
A dictionary d is created.
Dictionaries store data in key → value pairs.
Keys must be hashable (immutable types).
๐น 2. Creating a List as Key
key = [1, 2]
A list [1, 2] is created.
⚠️ Lists are mutable (can be changed later).
๐น 3. Assigning List as Dictionary Key
d[key] = "value"
Here Python tries to use the list as a dictionary key.
๐จ Problem:
Dictionary keys must be:
Immutable
Hashable
Lists are:
Mutable ❌
Not hashable ❌
๐ So Python raises an error:
TypeError: unhashable type: 'list'
๐น 4. Print Statement
print(d)
This line never executes because the program crashes earlier.
❗ Final Output
Error
TypeError: unhashable type: 'list'

0 Comments:
Post a Comment