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

Laknath Gunathilake
Laknath Gunathilake
1,860 Points

can't seem to get a full list from the dictionary keys

def courses(my_dict): slist=[] for value in my_dict.values(): slist.append(value) return slist

when I run this code, it says returned only five courses, but expected 18

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 most_classes(my_dict):
    max_teacher=''
    max_count=0
    for teacher in my_dict:
        count=len(my_dict[teacher])
        if count>max_count:
            max_teacher=teacher
            max_count=count
    return max_teacher 
def num_teachers(my_dict):
    return len(my_dict.keys())
def stats(my_dict):
    stat=[]
    for teacher in my_dict:
        count=len(my_dict[teacher])
        stat.append([teacher,count])
    return stat 
def courses(my_dict):
        slist=[]
        for value in my_dict.values():
             slist.append(value)
        return slist        

3 Answers

Steven Parker
Steven Parker
231,269 Points

Remember that each of the values in the dict is itself a list. When you append a list, you get only one new item.

:point_right: Instead of append, you might want to use extend.

This challenge can be a little tricky, but your code was real close- I tweaked it a little bit:

def courses(my_dict):
        slist=[]
        for teacher in my_dict.values():
            for value in teacher:
                slist.append(value)
        return slist
Aby Abraham
PLUS
Aby Abraham
Courses Plus Student 12,531 Points

My Answer:

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