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

teachers_stats.py - Challenge 3 of 5: def courses(teachers_courses):

See my code below. I was sure I had the right answer, since it was working in Workspaces, but I am getting a "Bummer! Couldn't find 'courses'" error when I submit my work. Any ideas as to why that'd be?

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(teachers_courses): #<- function that takes an argurement
    count = 0 #<- the initial counter
    for x in teachers_courses.keys(): #<- for each item in the keys of the dictionary
            count += 1 #<- add one to the count
    return count #<- show me the total count of keys

def num_courses(teachers_courses): #<- function that takes an argurement
    count = 0 #<- the initial counter
    for x in teachers_courses.values(): #<- for each item in values of the dictionary
        for i in x: #<- for each item in the list of the values of the dictionary
            count += 1 #<- add one to the count
    return count #<- show me the total count of values

def courses(teachers_courses):
    output = []
    for x in teacher_courses.values():
        output.append(x)
    return output

3 Answers

Taylor Schimek
Taylor Schimek
19,318 Points

In the for loop in courses(), you have teacher_courses. Should be teachers_courses

Thank you! I had defined a variable called teacher_courses in Workspaces and forgot to change it in the code challenge.

slavster
slavster
1,551 Points

William Spada Looks like we were using similar code and I was also having the same issue. I was getting an error saying that a list is not hashable.

I took a look at a few other questions in the community and saw a someone handling this correctly, see code below:

def courses(dic):
    listing = []
    for key, value in dic.items():
        for course in value:
            listing.append(course)
    return (listing)

My question is, why did I have to use .items() instead of .values() to get this to work? I would think the first iteration of code would do the job (after correcting for the teacher_courses mistake that Taylor Schimek pointed out). Can anyone explain this?

I think I get the logic of the function though. Since items() is being used, it includes teachers names as well. It's looking through each key and each value and then we specify an action when looping through the values. That action is creating the list.

I know this is late, but, why do you append?

Hey Jacky - "for x in teachers_courses.values()" is looping through each value (list of courses associated with each teacher) and setting that value equal to 'x'. I then need to add that value to a list, to store it for later. Creating an empty list in the beginning and using .append() works great here.

You could also use 'output += x' rather than .append().