-3

I would like to get the dictionary a using the dictionary b but it gives me an error.

My code :

a = {"c":"d"}
b = {{"a":"b"}:a}
print(b[{"a":"b"}])

My error :

File main.py in line 2
print(b[{"a":"b"}])
TypeError: unhashable type: 'dict'
  • 4
    You simply cannot use a dict as a dictionary key - it has to be something hashable, which generally implies being immutable. Ints, strings, tuples are the usual choices. – jasonharper Sep 02 '22 at 19:35
  • I am surprised the second line works. Or are you not showing the actual code causing the error since the error refers to line 2 which is actually line 3?! – luk2302 Sep 02 '22 at 19:37
  • [Here's](https://stackoverflow.com/q/14535730/12162258) a question about what "hashable" means in python for your perusal. – thisisrandy Sep 02 '22 at 19:39
  • https://stackoverflow.com/questions/13264511/typeerror-unhashable-type-dict – pippo1980 Sep 07 '22 at 17:17
  • Does this answer your question? [TypeError: unhashable type: 'dict'](https://stackoverflow.com/questions/13264511/typeerror-unhashable-type-dict) – pippo1980 Sep 07 '22 at 17:17

1 Answers1

2

You cannot use other dictionary as a key, u can only store a dictionary as a value!

example:

a = {
    "key": {"key":"value"}
}