I'm trying to make a dictionary so that each letter of the alphabet has its own sequence number, the problem is that the unhashable type: 'list'

 str1="абвгдеёжзийклмнопрстуфхцчшщъыьэюя" alf =[] for txt in str1: alf.append(txt) d = dict.fromkeys((([i for i in alf]),num for num in range(0,33))) print(d) 
  • If I’m not confusing anything, then in alphabet number 33 letters, and range (0.32))) the last element will be with index 31 - TEA
  • In addition to the answers from @ andy.37 and @MaxU: d = {chr(j) : i for i, j in enumerate(range(0x430, 0x450))} - greg zakharov

2 answers 2

 d = {i: l for i, l in enumerate(str1)} # d = {0: 'а', ...} 

or

 d = {l: i for i, l in enumerate(str1)} # d = {'а': 0, ...} 

But in general, it is enough (almost always) to use just enumerate(str1)

    You can use the Uicode code point as a sequence number:

     In [57]: d = {c:ord(c) for c in str1} In [58]: d Out[58]: {'а': 1072, 'б': 1073, 'в': 1074, 'г': 1075, 'д': 1076, 'е': 1077, 'ё': 1105, 'ж': 1078, 'з': 1079, 'и': 1080, 'й': 1081, 'к': 1082, 'л': 1083, 'м': 1084, 'н': 1085, 'о': 1086, 'п': 1087, 'р': 1088, 'с': 1089, 'т': 1090, 'у': 1091, 'ф': 1092, 'х': 1093, 'ц': 1094, 'ч': 1095, 'ш': 1096, 'щ': 1097, 'ъ': 1098, 'ы': 1099, 'ь': 1100, 'э': 1101, 'ю': 1102, 'я': 1103}