48
loading...
This website collects cookies to deliver better user experience
[2,4,6
] is a list and not a hashable type in Python. Dictionary keys must be immutable types, and the list is a mutable type.# Python TypeError: unhashable type: 'list'
fruits = {"Apple": 100, [2, 4, 6]: 'Qty'}
print("The fruits dictionary is ", fruits)
Traceback (most recent call last):
File "c:\Projects\Tryouts\listindexerror.py", line 3, in <module>
fruits = {"Apple": 100, [2, 4, 6]: 'Qty'}
TypeError: unhashable type: 'list'
# Fix TypeError: unhashable type: 'list'
fruits = {"Apple": 100, (2, 4, 6): 'Qty'}
print("The fruits dictionary is ", fruits)
The fruits dictionary is {'Apple': 100, (2, 4, 6): 'Qty'}
# Solution 2 TypeError: unhashable type: 'list'
fruits = {"Apple": 100, "Qty":[10,20,30]}
print("The fruits dictionary is ", fruits)
The fruits dictionary is {'Apple': 100, 'Qty': [10, 20, 30]}