Skip to content
Advertisement

Error when transforming (and reversing) Tuple Tuple to dict

I am trying to transform following tuple tuple into a dict (and a reversed one of that):

JavaScript

This is the code I use:

JavaScript

And I receive the following error:

TypeError: unhashable type: ‘list’

How should I do this? Thank you a lot!

Advertisement

Answer

you are right, lists cannot be used as keys in python dict because key has to be immutable. However, you can archive your goal by using tuples instead of lists (as tuples are immutable) To create your dict use

JavaScript

so that your key in dict is immutable tuple and then you will be able to use:

JavaScript

etc.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement