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.py - Teachers that teach the most_classes using Dictionaries

Please don't give me the answer, unless I am WAY OFF

Do I need to unpack the dictionary using (**key) or is that not necessary/suggested for this code challenge?

I think I'm having trouble with returning the correct teacher that associates with my max_count variable (which I attempted to create using a nested loop and the 'value in teacher_dict.values()' loop) -- Thank you!

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.

teacher_dict = {}

def most_classes(teacher_dict):
  max_count = 0
  for key in teacher_dict:
    teacher_name = key
    for value in teacher_dict.values():
      if len(value) > max_count:
        max_count = len(value)
        max_teacher = key
  return max_teacher
Chris Freeman
Chris Freeman
Treehouse Moderator 68,454 Points

Using double-asterisks is not needed for this challenge.

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,454 Points

The issue is in the nested for-loops. With the nesting loops, all of the values will be evaluated with the first key. What you want is to grab the key and value at the same time using .items():

  for key in teacher_dict:
    teacher_name = key
    for value in teacher_dict.values():

becomes

  for key, value in teacher_dict.items():
Peter Lynch
Peter Lynch
946 Points

Chris where did the course cover .item()? I would like to go over it again.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,454 Points

Peter, the dictionary method .items() wasn't covered in the stage Python Collections. I'm not sure which module it is covered it. I might have picked in up looking at the documentation for .values() after the video Dictionary Iteration. Sorry for getting ahead of the course material. If I come across the Treehouse lesson on .items() I'll post back.

Information on dictionary .keys(), .values(), and .items() can be found in the Python docs

Thank you so much, Chris! This makes so much sense, If only I knew that I could "grab" both the key and value using a single for loop! But, now I do!

You're Awesome Chris! Once again, thank you for all your help!