Python Forum
Unhashable error - Histogram code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unhashable error - Histogram code
#1
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'
Reply
#2
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}
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  You have any idea, how fix TypeError: unhashable type: 'list' lsepolis123 2 3,016 Jun-02-2021, 07:55 AM
Last Post: supuflounder
  TypeError: unhashable type: 'set' Stager 1 2,616 Jun-08-2020, 04:11 PM
Last Post: bowlofred
  Help with Plotting Histogram Shimmy 1 39,067 Oct-25-2019, 08:20 AM
Last Post: newbieAuggie2019
  How to plot histogram from 2 arrays? python_newbie09 5 8,815 Mar-28-2019, 04:20 AM
Last Post: scidam
  "Unhashable type: list" prasadhegde 8 6,490 Sep-10-2018, 03:09 PM
Last Post: buran
  TypeError: unhashable type : list yankeephan87 3 11,339 Jul-20-2018, 11:32 AM
Last Post: ichabod801
  How to: Plot a 2D histogram from N-dim array? StevenZ 1 2,487 Mar-31-2018, 04:08 AM
Last Post: Larz60+
  TypeError: unhashable type: 'list' Andreas 3 6,352 Jan-04-2018, 07:08 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020