0

i am trying to create a dictionary like something below:

frnd_detail1 = {
    {'name':'AD', 'age': 30}, {'name':'AKB', 'age':90},  {'name':'SD', 'age':40}
}

but i am getting the below error message as:

File "<ipython-input-72-a0d88fef112d>", line 4, in <module>
    {'name':'SD', 'age':40}

TypeError: unhashable type: 'dict'

why it is so, please provide some details

pault
  • 41,343
  • 15
  • 107
  • 149
  • 4
    Does this answer your question? [TypeError: unhashable type: 'dict'](https://stackoverflow.com/questions/13264511/typeerror-unhashable-type-dict) – awesoon Nov 25 '19 at 15:21

2 Answers2

0

You are creating a set (by accident I guess) by surrounding your dictionaries with curly braces {}.

In Python you can create sets using {} like this:

>>> my_set = {1,2,3}
>>> print(type(my_set))
<type 'set'>

Sets can only have values that are hashable. A dict isn't hashable which is why you get the error. You might want to create a list instead by replacing your outer {} with [] like this:

frnd_detail1 = [{'name': 'AD', 'age': 30}, {'name': 'AKB', 'age': 90}, {'name': 'SD', 'age': 40}]
SitiSchu
  • 1,361
  • 11
  • 20
0

I guess you want to create a dictionary of dictionaries or list of dictionaries.

If its a dict of dicts, then you need to give a key for its value, and not just ut each dictionary independently.

If its a list of dicts, you can append each dict into a list.

em_bis_me
  • 388
  • 1
  • 8