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

Frank Pizzuta
PLUS
Frank Pizzuta
Courses Plus Student 4,834 Points

why does it say there should be 18 returned items. In the dict given there is only 5 and my code returns all.

Dont understand why my code is not right. My dict has 5 classes per the commented description. The returned list has all 5 classes but when I check work it says there should be 18. Please advise.

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

def most_classes(my_dict):
    max_count  = 0
    for key in my_dict:
        if len(my_dict[key]) > max_count:
            max_count = len(my_dict[key])
            answer = key
    return answer

def stats(my_dict):
    my_list = []
    for key in my_dict:
        num_classes = len(my_dict[key])
        my_list.append([key,num_classes])
    return my_list

def courses(my_dict):
    mylist = []
    for item in my_dict.values():
        mylist.append(item)
    return mylist

my_dict = {'Jason Seifer': ['Ruby Foundations', 'Ruby on Rails Forms', 'Technology Foundations'],'Kenneth Love': ['Python Basics', 'Python Collections']}
print(most_classes(my_dict))
print(num_teachers(my_dict))
print(stats(my_dict))
print(courses(my_dict))

1 Answer

Martin Cornejo Saavedra
Martin Cornejo Saavedra
18,132 Points

You are not iterating over the list courses, you are just adding list of courses. This is one possible answer:

def courses(my_dict):
    mylist = []
    for courses in my_dict.values():
        for course in courses:
            if course not in mylist:
                mylist.append(course)

    return mylist