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

Code Review for Teacher Stats

FINISHED!!! Would anyone mind giving feedback, if any, on my code for this challenge? Any criticism is appreciated, thanks!

def most_classes(teach_dict):
  count = 0
  busy_teach = None
  for classes in teach_dict:
    if len(teach_dict[classes]) > count:
      count = len(teach_dict[classes])
      busy_teach = classes
  return busy_teach


def num_teachers(teach_dict):
  teach_count = len(teach_dict.keys())
  return teach_count


def stats(teach_dict):
  teach_list = []
  for classes in teach_dict:
    inner_list = []
    inner_list.append(classes)
    inner_list.append(len(teach_dict[classes]))
    teach_list.append(inner_list)
  return teach_list



def courses(teach_dict):
  course_list = []
  for classes in teach_dict.values():
    course_list.extend(classes)
  return course_list

1 Answer

Hi adudewithaview,

A suggestion on variable naming -

In most_classes() and stats() you've used the variable name classes but you're iterating over the teacher names in the dictionary. It would be more appropriate to use something like teacher to be more descriptive of what it's storing. In courses() it's more appropriate to use classes like you did because you're iterating over the values of the dictionary and those are in fact a list of classes.

I can see a few cases where you could shorten the code a bit or maybe just an alternative way of doing it.

def most_classes(teach_dict):

Since you need both the key and value inside your loop you could iterate over both the key and value. This makes the code inside the loop a little shorter and possibly more readable depending on personal preference.

def most_classes(teach_dict):
  count = 0
  busy_teach = None
  for teacher, classes in teach_dict.items():
    if len(classes) > count:
      count = len(classes)
      busy_teach = teacher
  return busy_teach

def num_teachers(teach_dict):

The length of a dictionary is the number of keys it has. So you could simply return the length of the dictionary that's passed in.

def num_teachers(teach_dict):
  return len(teach_dict)

def stats(teach_dict):

It's not strictly necessary to create the inner_list. Each time through the loop you can append a list consisting of the teacher and number of classes.

def stats(teach_dict):
  teach_list = []
  for teacher, classes in teach_dict.items():
    teach_list.append([teacher, len(classes)])
  return teach_list

If you learn about list comprehensions then this function could be even shorter. There's a workshop on comprehensions if you're interested in learning more. Python Comprehensions

def stats(teach_dict):
  return [[teacher, len(classes)] for teacher, classes in teach_dict.items()]

The code you wrote is fine. These are only suggestions and mostly a matter of personal preference. I hope it helps.

Excellent. Thanks for the help, Jason. This was informative.