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

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,868 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