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!

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

Stuck in Teacher Stats Q4

Can someone help me to figure out how to do challenge # 4 to list all courses offered by all teachers?

Additionally, I am struggling to figure out a program solution on top of my head with each challenge. I always need to go trial and error and change some lines of code before I finally got the correct solution. Are there ways to improve our brain mentally and be able to figure out a program in our head for different challenges with practice before we start coding?

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(teacherdict):
  newdict = {}
  for key, value in teacherdict.items():
      newdict[ len(value) ] = key
  return newdict[ max(newdict.keys()) ]

def num_teachers(teacherdict):
  return len(teacherdict)

def stats(teachers):
  num_classes = []
  for name, value in teachers.items():
    new_list = [name, len(value)]
    num_classes.append(new_list)
  return num_classes

def courses(teachers):
  class_list = []
  for value in teachers.values():

2 Answers

Aby Abraham
PLUS
Aby Abraham
Courses Plus Student 12,531 Points
def most_classes(dict):
    max_count = 0
    str = ""
    alist = dict.values()
    for key, value in dict.items():
        if len(value) > max_count:
            max_count = len(value)
            str = key
    return str


def num_teachers(dict):
    return len(dict.keys()) 


def stats(dict):
    slist = []
    for key, value in dict.items():
        slist.append([key, len(value)])
    return slist


def courses(dict):
    clist = []
    for value in dict.values():
        clist.extend(value)
    return clist
Steven Parker
Steven Parker
224,811 Points

Looks like you're almost there. You already extract the list of classes for each teacher into value.

:point_right: So now you just need to add the value list to the class_list (concatenate, not append).

Finally return the class_list.

Hi Steven, do I have to create another empty list to hold value inside for loop?