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

How to add dict key it to its own list, then append the sum of its values to same list, then add that list to new list?

Not sure what I am missing here.

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(teachers):
    return len(teachers)

def num_courses(courses):
    counter = sum(map(len, courses.values()))
    return(counter)

def courses(courses):
    course_list = []
    for course in courses.values():
        course_list += course
    return course_list

def most_courses(my_dict):
    max_count = 0
    max_class_teacher = ""
    for key,value in my_dict.items():
        temp_count = len(list(filter(bool, value)))
        if max_count < temp_count:
            max_count = temp_count
            max_class_teacher = key
    return max_class_teacher

1 Answer

John Lack-Wilson
John Lack-Wilson
8,181 Points

Hi Aaron, I assume its the final part of this challenge you're stuck on.

Here's the outline of one possible solution:

  • Define a stats function which takes the dictionary as a parameter
  • Create an empty list
  • Loop through the dictionary argument using the for key, value in dictionary.items(): style loop
  • Inside the for loop append a list containing the key and the length of the value like: lst.append([key, len(value)])
  • Return the new list

Hope that helps.