Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Question 3 of 5

Hi, I am stuck with question 3 of 5. What is wrong with my code? Thanks in advance.

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(dict):
    return len(dict)

def num_courses(dict):
    counter = 0
    for value in dict.values():
        counter += len(value)
    return counter

def courses(dict):
    list = []
    for value in dict.values():
        list = dict.extend(value)
    return list

1 Answer

Josh Keenan
Josh Keenan
19,437 Points

Here's my solution, it uses a for loop to iterate over the values (courses) each teacher has, then for each list of courses it starts a new for loop, to go through the list of their courses and then appends them to the course_list

def courses(arg):
    course_list = []
    for value in arg.values():
        for i in value:
            course_list.append(i)
    return course_list

Thank you!