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

FHATUWANI Dondry MUVHANGO
FHATUWANI Dondry MUVHANGO
17,796 Points

please help

hi i'm just missing one thing. i need to join each name with a corresponding number of classes the teacher has in the required format. im struggling to figure how to do that

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(dictionary):
    most_classes_count = 0
    most_classes_teacher = None
    for teacher in dictionary:
        if len(dictionary[teacher]) > most_classes_count:
            most_classes_count = len(dictionary[teacher])
            most_classes_teacher = teacher
    return most_classes_teacher

def num_teachers(dictionary):
    list_t = []
    for teacher in dictionary:
        list_t.append(teacher)
    return(len(list_t))

def stats(dictionary):
    list_of_teacher = []
    number_of_classes = []
    for teacher in dictionary:
        list_of_teacher.append([teacher])
    for value in dictionary.values():
        number_of_classes.append(len(value))
    return list(list_of_teacher) + list(number_of_classes)

4 Answers

Try changing append to extend.

def stats(dictionary):
    list_of_teacher = []
    number_of_classes = []
    for teacher in dictionary:
        list_of_teacher.append([teacher])
    for value in dictionary.values():
        number_of_classes.append(len(value))
    return list(list_of_teacher) + list(number_of_classes)

What you are doing is getting a list of all the teachers and then a list of all the class counts. Then you are returning the concatenation of those two lists.

What they want is a list of lists, each of the sublists should have a teacher and a class count in it.

def stats(dictionary):
    teacher_class_list = []
    for teacher in dictionary:
        teacher_class_list.append([teacher, len(dictionary[teacher])])
    return teacher_class_list
FHATUWANI Dondry MUVHANGO
FHATUWANI Dondry MUVHANGO
17,796 Points

thank you, it worked.

im doing the fourth question on the task, and this is my code: def courses(dictionary): teachers_courses = [] for values in dictionary.values(): teachers_courses.append(values) return teachers_courses

but it say that:"its getting 5 courses instead of 18", is there something wrong with my code?