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 (Retired) Dictionaries Teacher Stats

Yeeka Yau
Yeeka Yau
7,410 Points

courses function in Python Collections challenge

Hi, just wanted to get some help on the last function, it says my code is only returning 5 courses but expects 18 - I'm not too sure how to trouble shoot it. In my workspaces, it seems to react fine when I add in additional teachers and courses to a test dictionary.

Thanks for any help in advance!

teachers.py
# The dictionary will be something like:
# {'Jason Seifer': ['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations'],
#  'Kenneth Love': ['Python Basics', 'Python Collections']}
#
# Often, it's a good idea to hold onto a max_count variable.
# Update it when you find a teacher with more classes than
# the current count. Better hold onto the teacher name somewhere
# too!
#
# Your code goes below here.
def most_classes(dict):
  max_classes = 0
  for teacher in dict:
    classes = len(dict[teacher])    #returns the length of the value in each key
    if(classes>max_classes):
      max_classes = classes
      most_classes = teacher

  return most_classes

def num_teachers(dict):
  return len(dict)

def stats(dict):
  list = [[] for i in range(len(dict))]
  j = 0
  for key in dict:
      list[j].append(key)
      list[j].append(len(dict[key]))
      j=j+1
  return list

def courses(dict):
  course_list = []
  for teacher in dict:
    course_list.append(dict[teacher])

  return course_list  

1 Answer

Charlie Thomas
Charlie Thomas
40,856 Points
def courses(dict):
  course_list = []
  for teacher in dict.keys():
    for course in dict[teacher]:
      course_list.append(course)

  return course_list  

The problem is the dictionary follows this format:

{name: [courses]}

You were looping through each teacher then appending the list to course_list so course_list looked liked this: [[course1,course2],[course3,course4]]

. But you also have to loop through each course for each teacher aswell so your course_list looks like this: [course1,course2,course3,course4]