Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Collections (2016, retired 2019) Dictionaries Teacher Stats

Teacher Stats Question

Have the attached code.

Getting the following TypeError: unhashable type: list

Any thoughts? I assume its a simple fix.

teachers.py
def most_courses(teachers_courses):
    lng=0
    tc="no teacher"
    for teacher in teachers_courses:
        if len(teachers_courses[teacher])>lng:
            lng=len(teachers_courses[teacher])
            tc=teacher
    return tc

def courses(teachers_courses):
    lst=[]
    for value in teachers_courses.values():
        lst.extend(value)
    return lst

def num_teachers(teachers_courses):
    num=0
    for key in teachers_courses:
        num+=1
    return num

def num_courses(teachers_courses):
    num=0
    for value in teachers_courses.values():
        num+=len(value)
    return num

def stats(teachers_courses):
    lst=[]
    for teacher in teachers_courses.items():
        lst.append([teacher,len(teachers_courses[teacher])])
    return lst

1 Answer

Stuart Wright
Stuart Wright
41,120 Points

When iterating through dictionary items, you should have two different variables for key and value. You can call these whatever you want but k and v is common.

for k, v in teachers_courses.items():
     lst.append([k, len(v)])