+ 1
Ayuda-Help. Error: 'dict_items' object does not support indexing
I did a code for count the number letters using a 'dict'. Then I searched information in Google for turn the dict in a tuple and there to do a callable to the elements. But there is a fault. what am i doing wrong? https://code.sololearn.com/c3smxniY98tN/?ref=app
3 Antworten
+ 4
In line 9, just write
diccionario = tuple(diccionario.items())
dict.items() doesn't exactly create a tuple, but a view object. According to the documentation, it gives "a dynamic view on the dictionary’s entries, which means that when the dictionary changes, the view reflects these changes." Tuples, in contrast, are immutable.
https://docs.python.org/3/library/stdtypes.html#dict-views
Also, dict.items() itself is a callable object, so if you just want to print the key-value pairs, instead of converting it to a tuple you could directly do
for i in diccionario.items():
print(i)
+ 3
Cadvortex I'm glad I could help. 😊
+ 1
Oh man, thank you, I was lost with that code.