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

Dictionary - counting the max element and returning a value of the key

Hi,

I tried the last function in python shell and it worked but i'm not sure what am i missing here (function name: most_courses)

The question: Create a function named most_courses that takes our good ol' teacher dictionary. most_courses should return the name of the teacher with the most courses. You might need to hold onto some sort of max count variable.

Thank you so much!

teachers.py
# 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(arg):
    return len(arg)

def num_courses(arg):
    total = 0
    for value in arg.values():
        total += len(value)
    return total

def courses(arg):
    course_name = []
    for course_list in arg.values():
        for course in course_list:
            if course not in course_name:
                course_name.append(course)
            else:
                continue

    return course_name

def most_courses (arg):
    max_name = None
    max_count = 0
    for teacher in arg.keys():
        for course_list in arg.values():
            if len(course_list) > max_count:
                max_count = len(course_list)
                max_name = teacher
            else:
                continue

    return max_name

1 Answer

Did you ever figure this out?