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 trialDhruv Ghulati
1,582 PointsNot 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):
max_count=0
for key in my_dict:
count=sum(value)
if count>max_count:
max_count=count
return max_count
1 Answer
William Li
Courses Plus Student 26,868 PointsThat'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
1,582 PointsDhruv Ghulati
1,582 PointsThis 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
Treehouse Guest TeacherKenneth Love
Treehouse Guest TeacherI don't think we've actually covered
.items()
at this point in the course. You can, of course do it like this, too: