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

Travis John Villanueva
Travis John Villanueva
5,052 Points

stats.py

What does unhashable type list means? I tested in my workspace and it runs fine

teachers.py
# The dictionary will look something like:
# {'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics'],
#  'Kenneth Love': ['Python Basics', 'Python Collections']}
#
# Each key will be a Teacher and the value will be a list of courses.
#
# Your code goes below here.
def num_teachers(x):
    y=x.keys()
    return len(y)
def num_courses(x):
    y=0
    for v in x.values():
        if type(v) == list:
            for d in v:
                y+=1
        else:
            y+=1
    return y
def courses(x):
    y=0
    h=[]
    for v in x.values():
        if type(v) == list:
            for d in v:
                h.append(d)
        else:
            h.append(v)
    return h

def most_courses(X):
    MAX_COUNT=0
    WINNER=""
    for teacher, courses in X.items():
        if len(courses)>(MAX_COUNT):
            MAX_COUNT=len(courses)
            WINNER=teacher
    return WINNER 

def stats(X):    
    newX={}
    for teacher in X.keys():
        num_of_courses=0    
        for courses in X[teacher]:    
            num_of_courses += 1
        newX[teacher]=num_of_courses
    return newX

1 Answer

Alex Koumparos
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Koumparos
Python Development Techdegree Student 36,887 Points

Hi Travis,

In order for your code to work, your stats() function must return a list of lists. Here are the instructions again:

stats should return a list of lists where the first item in each inner list is the teacher's name and the second item is the number of courses that teacher has. For example, it might return: [["Kenneth Love", 5], ["Craig Dennis", 10]]

And here is the output your function produces for the same input:

{'Kenneth Love': 5, 'Craig Dennis': 10}

Hope that points you in the right direction.

Cheers

Alex