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

Oprea Mihai
Oprea Mihai
5,592 Points

stats

I can't figure out last part of this task: Can you help me ?

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(name):
    return len(name)
def num_courses(name):
    i = 0
    for value in name.values():
        i += len(value)
    return i
def courses(name):
    course_list = []
    for value in name.values():
        for course in value:
            course_list.append(course)
    course_list = set(course_list)
    return course_list
def most_courses(name):
    list_teach={}
    for key ,value in name.items():
            course_l=len (value)
            nb_course = course_l
            list_teach.update({key: nb_course})
    list_teach=max(list_teach.items() , key=lambda k:k[1])
    list_teach=list_teach[0]

    return list_teach
def stats(name):
    list_teach = {}
    list_2=[]
    for key, value in name.items():
        course_l = len(value)
        #nb_course = course_l
        list_1=[key,course_l]
        list_2.append(list_1)
        list_teach.update(list_2)
       return list_teach

1 Answer

Hi there, your very nearly there I think. Does the below make sense?

def stats(t_dict):
    s_list = []                             # create the list to return
    for k in t_dict.keys():                 # iterate through the passed in dict keys
        s_list.append([k, len(t_dict[k])])  # append the key (teachers nane and the len of the key value)
    return s_list