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

A quiz Question!

It's official: you're awesome at Python dictionaries! One last task and then you can take a well-deserved break! In this last challenge, I want you to create a function named stats and it'll take our teacher dictionary as its only argument. stats should return a list of lists where the first item in each inner list is the teacher's name and the second item is the number of courses that teacher has. For example, it might return: [["Kenneth Love", 5], ["Craig Dennis", 10]]. I wonder what is wrong with my code! Please 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 stats(techdict):
    lista=[]
    for items in teachdict.items():
        lista.append(items)
    return lista






def most_courses(teacher):
    leader = 0
    teacherMost = ""
    for i in teacher:
        # if i in teacher[i] > leader:
        if len(teacher[i]) > leader:
            leader = len(teacher[i])
            teacherMost = i # Since i is the key, don't need teacher.value()
        else:
            continue
    return teacherMost






def courses(c):
    teachcourse=[]
    for teach in c.values():
        teachcourse.extend(teach)
    return teachcourse





def num_courses(b):
    courses=0
    for course in b.values():
        courses = courses + len(course)
    return courses







def num_teachers(a):
    teachers=0
    for teacher in a:
        teachers= teachers + 1
    return teachers

3 Answers

Hi there,

I see with your stats function is not returning what the task want you to do.

def stats(techdict):
    lista=[]
    for items in teachdict.items():
        lista.append(items)
    return lista

this task wants you to return a list that contains a list with teacher's name and length of course. What your code will do is return lista which is a list that contains results of teach.items(). That is not what the task is asking here.

What you want to do should somewhat look like this.

def stats(teachdict):
    lista = []
    for key in teachdict:
        course = [key, len(teachdict[key])]
        lista.append(course)
    return lista
Herman Brummer
Herman Brummer
6,414 Points

This is how I counted the number of teachers:

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

Here is how I did it

def stats(my_dict):
    list = []
    for teacher, course_list in my_dict.items():
        num_course = len(course_list)
        list.append([(teacher), num_course])
    return list