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

Teachers stats - Challenge 4

I can't figure out why courses isn't correct. Any pointers?

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_in):
    most_classes = 0
    busiest_teacher = ''
    for entry in dict_in:
        if len(dict_in[entry]) > most_classes:
            most_classes = len(dict_in[entry])
            busiest_teacher = entry
        else:
            continue
    return busiest_teacher

def num_teachers(dict_in):
  return len(dict_in)

def stats(dict_in):
  newlist = []
  for key, value in dict_in.items():
    newlist.append([key, len(value)])  # append [<name>, <number of classes>] to newlist
  return newlist

def courses(dict_in):
  list = []
  for key, value in dict_in.items():
    list.extend(dict_in[value])
return list

4 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

This should do it:

def courses(dict_in):
  course_list = []
  for key, value in dict_in.items():
    list = value;
    for newItem in list:
      course_list.append(newItem)
  return course_list

Well using .values I was able to make this work:

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

Nice. More elegant than mine!

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

Suggestion: when pasting in code, it's nice if it is formatted properly. What you need to do is spelled out in the Markdown Cheatsheet (link below). But basically, be sure and leave a blank line before and after the code you paste in, and a line just above your code that is 3 accent signs and the type of code, and a line after your code and above the blank line that is another 3 accent signs. Like this, except I have to use --- here instead of three accents:

                   //blank line
---python
. . . code. . .
---
                   //blank line