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 (Retired) Dictionaries Teacher Stats

Course Python Collections

Hi, I am facing some difficulty in getting a single list of all courses thought by all teachers. Given below is my code (function)

def courses(arg1): course_list = [] for value in arg1: for courses in value: course_list.extend(courses)

return course_list 

Output: You returned 69 courses

teachers.py
# The dictionary will be something like:
# {'Jason Seifer': ['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations'],
#  'Kenneth Love': ['Python Basics', 'Python Collections']}
#
# Often, it's a good idea to hold onto a max_count variable.
# Update it when you find a teacher with more classes than
# the current count. Better hold onto the teacher name somewhere
# too!
#
# Your code goes below here.
def courses(arg1):
    course_list = []
    for value in arg1: 
        for courses in value:
            course_list.extend(courses)

    return course_list    
def stats(arg1): 
    list_teachers = []
    for key, value in arg1.items():
        list_teachers.append([key, len(value)])
    return list_teachers    

def num_teachers(arg1): 
    number_of_teachers = 0
    for key in arg1:
       number_of_teachers += 1
    return number_of_teachers

def most_classes(arg1): 
    max_classes = 0
    for key, value in arg1.items(): 
        if len(value) > max_classes:
            max_classes = len(value)
            teacher = key
    return teacher

2 Answers

Aby Abraham
PLUS
Aby Abraham
Courses Plus Student 12,531 Points
def most_classes(dict):
    max_count = 0
    str = ""
    alist = dict.values()
    for key, value in dict.items():
        if len(value) > max_count:
            max_count = len(value)
            str = key
    return str


def num_teachers(dict):
    return len(dict.keys()) 


def stats(dict):
    slist = []
    for key, value in dict.items():
        slist.append([key, len(value)])
    return slist


def courses(dict):
    clist = []
    for value in dict.values():
        clist.extend(value)
    return clist
Steven Parker
Steven Parker
230,995 Points

:point_right: If you want only the values from a dictionary, you can use the .values() method.

    for value in arg1.values():