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

Returning a single list

I am trying to return a single list from a dictionary, but i can't get the code to pass task 3

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(teacher):
    numOfTeachers = len(teacher)
    return numOfTeachers

def num_courses(teachers):
    num_courses = 0
    for teacher in teachers.values():
        num_courses += len(teacher)
    return num_courses     

def courses(teachers):
    for course in courses.values():
    teacher = list.append(teachers)
    return teacher

1 Answer

Steven Parker
Steven Parker
230,274 Points

It looks like you have a good strategy, but a few implementation issues:

  • the incoming parameter is "teachers" but the loop uses "courses" (which is not defined)
  • statements inside a loop must be indented more than the loop itself
  • the word "list" is a language keyword so perhaps not a good choice for a variable name
  • you may need to initialize a new list before you add to it
  • the "append" function will create a nested list — you might want "extend" instead
  • inside the loop, you'll probably want to use the loop variable "course" instead of the whole dictionary
  • you won't need a "teacher" variable since you'll be returning the new list itself