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

Jen Farrant
Jen Farrant
6,905 Points

teachers 2/5 - almost there, but not quite! Please help

When I use print, instead of return - I can see that my code isn't just creating a single list, with a single length, it is creating a list for each teacher in the course

So calculating the length of the list results in multiple calculations- one for each list.

I guess it is because it is running the code for each key, rather than in total, but I don't know why.

Any help would be appreciated! Thank you!

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):

    print(teachers)
    word_count =0
    for teacher in teachers:
        word_count +=1
    return word_count

def num_courses(teachers):

    all = []
    for value in teachers.values():
        all.extend(value)
        return len(all)

2 Answers

Feisty Mullah
Feisty Mullah
25,849 Points

Hello Jen, You be able to pass the challenge by adding (+=) instead.

def num_teachers(teachers):
    print(teachers)
    word_count =0
    for teacher in teachers:
        word_count +=1
    return word_count

def num_courses(teachers): 
    courses = [] 
    for course in teachers.values(): 
        courses += course 
    return len(courses)
Jen Farrant
Jen Farrant
6,905 Points

thank you very much Isna!