0

i´ve trying to add and dictionary to a set, but theres some error getting me some troubles, i know that there is because of a Key not hasheable but i cannot find it

import openpyxl
from openpyxl import Workbook


bookSource = openpyxl.load_workbook('J:\BI_Proyect\sources\soucesInXLSX.xlsx')

ProgramasxUni = bookSource["ProgramasPorUniversidades"]
Ranking = bookSource["Universidades"]

def searchInRanking(nameUni, Uni):
    for i in range(2, 574):
        if(Ranking[f"B{i}"].value == Uni["Nombre"]):
            Uni["fk_InternationalScore"] = Ranking[f"A{i}"].value
            #Uni.update({"fk_InternationalScore": Ranking[f"A{i}"].value})
        else:
            Uni["fk_InternationalScore"] = None


thisSet = set()


for i in range(2, 255):

    NAMEUNI=ProgramasxUni[f"A{i}"].value
    Universidad = {
        "Id" : i-1,
        "Nombre": NAMEUNI,
    }
    searchInRanking(NAMEUNI, Universidad)
    print(Universidad)
    thisSet.add(Universidad)
    print(thisSet)

print(thisSet)

printing the dict is something like this:

{'Id': 1, 'Nombre': 'Albright College', 'fk_InternationalScore': None}

And executing the code gives me this respond:

PS J:\BI_Proyect> & C:/Python39/python.exe j:/BI_Proyect/python_scripting/Universidades.py
{'Id': 1, 'Nombre': 'Albright College', 'fk_InternationalScore': None}
Traceback (most recent call last):
  File "j:\BI_Proyect\python_scripting\Universidades.py", line 31, in <module>
    thisSet.add(Universidad)
TypeError: unhashable type: 'dict'
LEx233
  • 1
  • Does this answer your question? [Add a dictionary to a \`set()\` with union](https://stackoverflow.com/questions/34097959/add-a-dictionary-to-a-set-with-union) – Zeromika Jun 02 '21 at 02:37
  • You can't add dictionaries to sets because they are unhashable, which is a requirement to be a [set member](https://docs.python.org/3/library/stdtypes.html#set-types-set-frozenset). You will need to convert the dictionaries into something else which is hashable. – martineau Jun 02 '21 at 02:43
  • You can make dictionary into a string and then save it in set. – Yoshikage Kira Jun 02 '21 at 02:49

0 Answers0