Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Collections (Retired) Dictionaries Teacher Stats

Quick question over about my code (dictionaries)

I can't seem to get this to work as it is - would I be better off always using a for loop in these type of situations?

teachers.py
def most_classes(my_dict):
    values = list(my_dict.values())
    my_idex = values.index(max(values))
    myKey = list(my_dict.keys())
    return myKey[my_idex]
Chokdee Srisuk
Chokdee Srisuk
19,380 Points

Yes, use for loop to iterate over keys in dictionary which I name key as teacher. for teacher in my_dict:

1 Answer

Stephen Link
Stephen Link
3,685 Points

What you have would be fine as it is for a simple dictionary where each key has a single integer value. However, the problem indicates that your dictionary is going to be more complicated than that. Each teacher will have a list of classes that they teach and you'll need to determine how many classes a teacher teaches based on that list.