+ 1

Why there is an error

bad_dict = { [1, 2, 3]: "one two three", }

26th May 2017, 4:18 PM
jay dave
jay dave - avatar
3 Answers
+ 4
because dictionaries only can have immutable keys, int, strings and tuples by example. in your example, the list [1,2,3] is a invalid key because the list is a mutable data type.
26th May 2017, 4:33 PM
Gytree
Gytree - avatar
+ 2
The key of dictionary should be hashable, but list is unhashable so that code gives you error.
26th May 2017, 4:31 PM
OrbitHv [Inactive]
OrbitHv [Inactive] - avatar
0
This appears (to me) to mix the syntax for dictionaries: {index:value, index:value, ...} lists...in this case I'd expect it on the right hand side of equals (=): ... = [value, value, value] and assignment (left-hand side of equals): a = value a,b = value1,value2 [a,b] = [value1, value2] *** Note...this does NOT fix it: { [1,2,3]:["a","b","c"] } TypeError: unhashable type: 'list' Which brings me back to mixing syntax.
26th May 2017, 4:37 PM
Kirk Schafer
Kirk Schafer - avatar