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

I don´t understand how i'm supposed to do this task.

i´ve been trying for a while but i can´t find out how to do it, please someone tell what do i have to do.Please don´t give me any answers. Thank you Nicolas

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(dict):
    count = 0
    for key in dict.keys():
        count += 1
    return count

def num_courses(dict):
    count = 0
    for value in dict.values():
        count += len(value)
    return count

def courses(dict):
    names = []
    for value in dict.values():
        names.append(value)
    names = sum(names, [])
    return names

def most_courses(dict):

This is what i have to do:

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.

1 Answer

Steven Parker
Steven Parker
231,248 Points

Good attitude. :+1: I only give hints anyway.

So here's a few:

  • the instructions already suggested you might need a "max count" variable
  • you might also need a "teacher with most" variable
  • you might use a loop to go through the items in the dictionary
  • for each item, if the course count is larger than the stored "max", you could update the max and the teacher
  • then when the loop ends, you could return the last saved teacher

Thank you, it worked

Suwijuk Nawaphanarat
Suwijuk Nawaphanarat
2,103 Points

Hello, Mr Steven.

I managed to find the max count of the courses. However, I cannot return with a teacher name with the most courses. Here are my codes:

def most_courses(courses): most_courses = [] for key in courses.keys(): most_courses.append(len(courses[key])) return max(most_courses)

Can you please help me? Thanks!

Steven Parker
Steven Parker
231,248 Points

Take another look at the hints I gave to Nicolás, plus:

  • you won't need to create a new list
  • you only need to save the largest count seen and the teacher that goes with it
  • after the loop you will return the teacher