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

A quiz question!

Wow, I just can't stump you! OK, two more to go. I think this one's my favorite, though. 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. I'm pretty confused right now. Could you give some instructions?

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 most_courses(dictionary):










def num_teachers(a):
    teachers=0
    for b in a:
        teachers=teachers + 1
    return teachers
def num_courses(c):
    courses=0
    for num_courses in c.values():
        courses= courses + len(num_courses)
    return courses
def courses(d):
    course=[]
    for e in d.values():
        course.extend(e)

    return course

2 Answers

Hi there,

For this one, I created two variables, one to hold the current max number of courses; initialised to zero, and a string to hold the teacher name, initialised to a blank string.

I then looped through dict.items() (dict is the name of the dictionary passed into the method) storing both the key and value. I used teacher and courses to hold each key/value pair within the for loop.

I then tested to see if len(courses) was greater than the value held in the max courses variable from the first paragraph here. If it was, I stored the value held in teacher into the string I initialised at the beginning and also updated the max courses variable to reflect the current 'leader'.

At the end of the loop, I returned the value held by the string variable I created at the beginning of the method.

I hope that helps.

Steve.

Hi there!

Just to rephrase the question: There's a dictionary like the one in the comments at the top of the file where each key in the dictionary is a teacher, and each value is a list of courses. You just have to go through and return the key that holds the longest list (if two are the same length, the checker won't mind what you pick). If you wanted to get really adventurous you could look into how the max() function could be used for this, but a loop of some sort would also suffice :)