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 trial

Python Python Collections (Retired) Dictionaries Teacher Stats

lindseyk
lindseyk
4,506 Points

Python Collections: teachers_stats challenge, Task 1 of 4. I passed, but wonder if there is a cleaner alternative?

Hi there! Just throwing this question out there. I completed task 1 of 4 in the teachers_stats challenge, and my code was successful! However, it seems like my logic is rather complicated. I am wondering if anyone has a more simple alternative? Attaching my code below for discussion. Thanks!!

teachers.py
# 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(teacherdict):
    newdict = {}
    for key, value in teacherdict.items():
        newdict[len(value)]= key
    most = list(sorted(newdict.keys()))
    return newdict[most[-1]]

2 Answers

Emil Rais
Emil Rais
26,873 Points

You need to return the teacher who teaches the most classes. What you do is create a dictionary from the number of classes to teacher. You sort the dictionary on the keys, turn it into a list and grab and return the teacher that is last in the list. It is certainly a solution.

A simpler solution is to create a pair (teacher, numberOfClasses). Then for each iteration on the input dictionary you update that pair to contain the teacher with the highest number of classes of the current "best" teacher and the one examined in the iteration. Does that make sense? Then when you're done with the loop you'll know that the teacher in your pair is the right one and so you can return it.

lindseyk
lindseyk
4,506 Points

Thank you! Yes, it seems this is the sort of logic that was expected in the challenge. I will give it a try! Much appreciated.

I have done this task before but never used the sorted function. Although since your are converting the dictionary values into keys and in turn the keys into a list you could use the max function to achieve the result with even less code.

def most_classes(teacherdict):
    newdict = {}
    for key, value in teacherdict.items():
        newdict[len(value)]= key
    return newdict[max(newdict.keys())]
lindseyk
lindseyk
4,506 Points

Oh, I had no idea there is a max() function! (I am very new at this.) Thank you!!