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

I have a couple of doubts here. Could someone help please

Create a function named most_classes that takes a dictionary of teachers. Each key is a teacher's name and their value is a list of classes they've taught. most_classes should return the teacher with the most classes.

A friend of mine wrote this code down for me but i'm not sure as to why certain things were written the way were written. I need a little explanation as to 1) Why is there a max_count = 0 2) and why max_teacher is equal to an empty string

Any input would be much appreciated Thank you

# 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(teachers):
    max_count = 0
    max_teacher = ''
    for key in teachers:
        if len(teachers[key]) > max_count:
            max_count = len(teachers[key])
            max_teacher = key
    return max_teacher

2 Answers

Jennifer Nordell
Jennifer Nordell
Treehouse Teacher

Hi there!
This is all based on fictional data and nothing actually gathered from TTH regarding the number of courses any given instructor teaches

Ok so let's say you were doing this manually with a pen and paper (which is tedious but possible). And let's say you were picking up a piece of paper on which was written a teacher and the list of classes. For the sake of this example, let's say you're not familiar with this school and have no idea what the teacher's names even are. So you start with paper #1 and on it is written "Dave McFarland", and he teaches 8 courses. Well now you can write down on a scratch paper the name of the teacher because you now know one, and he teaches the most of any teachers you've reviewed (because you have only reviewed one)! So the current number of courses to beat is 8.

And you make your way through another few names, but none of them teach more than Dave yet. But finally, you get to someone who does. Let's say Alena Holligan and she teaches 11! So now you can scratch out Dave and replace it with Alena. And the new number of courses to beat is 11.

Those non-initialized variables with the empty string and the max_count are the equivalent of the scratch paper here. Every time you find a new instructor who beats the current record for courses taught, you replace the name and the number of courses with the new record. When you're done with the stack of papers, the last instructor and number of courses you wrote down will be the "winner".

Hope this helps! :sparkles:

Lukas Hölzer
Lukas Hölzer
37,349 Points

Maybe my solution will help you.

def most_classes(teachers):
    max_count = 0
    for key, value in teachers.items():
        if len(value) > max_count:
            max_count = len(value)
            teacher_with_most_classes = key
    return teacher_with_most_classes