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

Cole Diemer
Cole Diemer
752 Points

Dictionary iterations step 4 of 4?

cant figure out how to create the courses function.

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.
my_dict = {'Jason Seifer': ['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations'],
           'Kenneth Love': ['Python Basics', 'Python Collections']}

def most_classes(my_dict):
  largest = 0
  for key in my_dict:
    if len(my_dict[key]) > largest:
      largest = len(my_dict[key])
      busy_teacher = key
  print(busy_teacher)
  return busy_teacher  
most_classes(my_dict)


def num_teachers(my_dict):
  number = 0
  for key in my_dict:
    number += 1
  return number
num_teachers(my_dict)

def stats(my_dict):
  result = []
  for key in my_dict:
    result.append([key, len(my_dict[key])])
  return result

def courses(my_dict):
  resultz = []
  for course in my_dict:
    resultz.append(my_dict.values())

1 Answer

There is a little bit of wonkiness in a couple places

You're loop is over the teachers not the courses as they are the keys in the dictionary. You need to get the courses out of the lists they are in when they are values in the dictionary. You also appear to be appending all of the values in the dictionary at once multiple times. You also need a return statement.

def courses(my_dict):
  resultz = []
  for teacher in my_dict:
    courses=my_dict[teacher]
    ....
  return resultz