0

I got a problem: I want to save lists in a dictionary like that:

EventFields: dict = {[0, 1]: 'specialField'}

Why does this not match? And what's the solution?

Thanks

Em J0
  • 3
  • 1
  • 2
    You can use tuples instead of lists...so `(0, 1)` instead of `[0, 1]`. – Mark Dec 27 '21 at 01:38
  • 1
    Welcome to Stack Overflow. Please read [ask] and https://meta.stackoverflow.com/questions/359146. What do you mean by "not match"? What problem are you trying to solve? While I happen to recognize what you are doing (so I could link you to a duplicate), it's important to ask questions clearly and state the actual problem. Developing this skill will also help you find answers with a search engine - whether you search for the [problem](https://duckduckgo.com/?q=TypeError%3A+unhashable+type%3A+%27list%27) or the [goal](https://duckduckgo.com/?q=python+use+list+as+dict+key). – Karl Knechtel Dec 27 '21 at 01:38

1 Answers1

0

Lists aren't hashable, since you can mutate a list (e.g. by doing something like list[0] = 1).

You would want to cast the list to a tuple:

dict = {(0, 1): 'specialField'}
BrokenBenchmark
  • 18,126
  • 7
  • 21
  • 33