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

Dhruv Ghulati
Dhruv Ghulati
1,582 Points

Not sure how to get key of a value, and know dictionaries don't allow you to do this - what is a better way of framing?

I think I have the max_count part ok, providig I can loop through and sum(value) (unless I should be count(value)) - but not sure how to return key essentially

# 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(my_dict):
  for key in my_dict:
    if count>max_count:
  return max_count

1 Answer

William Li
William Li
Courses Plus Student 26,865 Points

That's because this function is supposed to return a teacher's name, not the max count of their classes.

I modified your code a bit, it should work now.

def most_classes(my_dict):
  max_count = 0
  teacher = ""
  for key, value in my_dict.items():
    if len(value) > max_count:
      teacher = key
      max_count = len(value)
  return teacher
Dhruv Ghulati
Dhruv Ghulati
1,582 Points

This is awesome. I didn't know you could reference 2 things in a for loop till now! Also, len(value) is interesting in that it is the count of the values, not the length of the values themselves which you can see is confusing. Thanks dude!

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

I don't think we've actually covered .items() at this point in the course. You can, of course do it like this, too:

for teacher in my_dict:
    if len(my_dict[teacher]) > max_count