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

samuel fiori
samuel fiori
4,497 Points

NEED HELP with last Challange of Collections ( most_courses)

I tried it for a while now but I just can't make it on my own. I have to create รก function that returns the name of the treehouse teacher with the most courses out of a DICT(TEACHER=[COURSES]). Sounds pretty simple but I just don't get how to get the max value out of a list and the max() method just don't want to work, so what am I doing wrong? Thank's for help!

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.

# returns the number of teachers

def num_teachers(dic):
    teachers = 0
    for key in dic.keys():
        teachers += 1

    return teachers

# returns the absolut number of courses

def num_courses(dic):
    courses = 0
    for value in dic.values():

        courses += len(value)
    return courses

# returns all courses added in a single list

def courses(dic):
    list = []
    count = 0
    for value in dic.values():
        list.append(value)
    other = []
    for i in list:
        other.extend(i)

    return other

# returns the teacher with the most courses

def most_courses(dic):
    v = []
    len = []
    k = []
    count = 0
    for value in dic.values():
        v.append(value)
        len.append(len(v[count]))
        count += 1
    for key in dic.keys():
        k.append(key)

    return k[len.index(max(len))]

2 Answers

Charlie Gallentine
Charlie Gallentine
12,092 Points

Here's how I solved it:

def most_courses(arg):
    most_course = 0 # A variable to check if someone has more courses
    newTeacher = "" # A place to store their name if they have more 
    for teacher in arg: # Loops through the given dictionary
        if len(arg[teacher]) > most_course: # Checks if one has more courses
            most_course = len(arg[teacher]) # Updates who has more
            newTeacher = teacher # Updates teacher name
    return newTeacher # Returns teacher's name
samuel fiori
samuel fiori
4,497 Points

Nice code & Thanks for the help!!