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 (2016, retired 2019) Dictionaries Teacher Stats

victor E
victor E
17,741 Points

I am completely drawing a blank on this one...

some tips would be great, you dont need to post answer please. thank you in advance.

# The dictionary will look something like:
# {'Andrew Chalkley': ['jQuery Basics', 'Node.js Basics'],
#  'Kenneth Love': ['Python Basics', 'Python Collections']}
# Each key will be a Teacher and the value will be a list of courses.
# Your code goes below here.
def num_teachers(dictionary):
    return len(dictionary)

def num_courses(dictionary):
    counter = 0
    for value in dictionary.values():
        counter += len(value)
    return counter
def courses(dictionary):
    library = []
    for items in dictionary.values():
    return library 
def most_courses(dictionary):

1 Answer

Josh Keenan
Josh Keenan
19,652 Points

Here's my solution:

def most_courses(teachers):
    max = 0
    teacher = ""
    for key, value in teachers.items():
        if len(value) > max:
            max = len(value)
            teacher = key
    return teacher

We are creating two variables, and I am sure there is a cleaner way to do this, but we have the max number of courses and a teacher to hold the current maximum and the teacher with it. Then we are iterating through the contents of the dictionary and checking if the current teacher has more courses than the max; if so we set the max to that and the teacher to whichever teacher it was. Hope this helps!

victor E
victor E
17,741 Points

that makes sense! thank you for writing an explanation as to how the code is running!