Python Forum

Full Version: Unhashable error - Histogram code
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello guys!

I want to get the weekday part from each list in the emailtext list and add it to a histogram.

emailtext = [['From', '[email protected]', 'Mon', 'Jan', '5', '09:14:16', '2008'],['From', '[email protected]', 'Fri', 'Jan', '4', '18:10:48', '2008']]

weekdays = dict()

#creates list with weekdays and adds it to a dictionary 
for i in emailtext:
    print(i[2])
    weekdays[i] = weekdays.get(i[2],0) + 1
I get the correct print values on my prompt, but I can't figure out a way to add them directly to my dictionary without having them go to an intermediary list, and then going through that intermediary list again.

Error:
Traceback (most recent call last): File "C:\Users\lucas\OneDrive\Desktop\Aulas\FreeCodeCamp\Scientific Computing with Python\Exercise 9.2 - forum", line 35, in <module> weekdays[i] = weekdays.get(i,0) + 1 TypeError: unhashable type: 'list'
emailtext = [
    ["From", "[email protected]", "Mon", "Jan", "5", "09:14:16", "2008"],
    ["From", "[email protected]", "Fri", "Jan", "4", "18:10:48", "2008"],
]

weekdays = dict()

for items in emailtext:
    day = items[2]
    weekdays[day] = weekdays.get(day, 0) + 1


print(weekdays)
Output:
{'Mon': 1, 'Fri': 1}