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

List of lists function

Guys, I need a hint on how to approach this step. Here is the task:

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 in the number of courses that teacher has. For example, it might return: [["Kenneth Love", 5], ["Craig Dennis", 10]]

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.
import collections

def num_teachers(teacher_dict):
    return len(teacher_dict)

def num_courses(teacher_dict):
    count = 0
    for arr in teacher_dict.values():
        for item in arr:
            count += 1
    return count

def courses(teacher_dict):
    courses = []
    for arr in teacher_dict.values():
        for item in arr:
            courses.append(item)
    return courses

def most_courses(teacher_dict):
    teachers_list = {}
    for teacher in teacher_dict:
        teachers_list[teacher] = len(teacher_dict[teacher])
    return max(teachers_list, key=teachers_list.get)

def stats(teacher_dict):
    big_list = []
    small_list = []


    return big_list

6 Answers

Slava Serbov
Slava Serbov
18,398 Points

For me this work like a charm. Clean and simple.

def stats(t_dict):
    all_courses = []
    for teacher in t_dict:
        all_courses.append([teacher, len(t_dict[teacher])])
    return all_courses

?Don't forget to upvote if this helps you.?

Erik Burmeister
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Erik Burmeister
Python Web Development Techdegree Graduate 17,108 Points

Slava, your code right there is top notch. Thank you for showing it. I'm still trying to wrap my head around this whole concept, but seeing that it doesn't need to be a complicated piece of code is helping it sink in.

Here's my solution that works! It took me a while to figure out, but I was able to utilize my code from the previous steps.

def stats(abs):
    statslist = []
    for teacher, courses in abs.items():
        total = 0
        for value in abs.values():
            for course in value:
                total += 1
                statslist.append([teacher, total])
    return statslist
Umesh Ravji
Umesh Ravji
42,386 Points

Hey there, if you want a hint I would have to give you this:

for teacher, courses in teacher_dict.items():
    number_of_courses = 42  # calculate using courses list here
    big_list.append([teacher, number_of_courses])

Wouldn't worry about the small_list, but you can have it if you want :)

Hi Umesh,

Thank you for your response.

Here's my another attempt:

for teacher, courses in teacher_dict.items(): number_of_courses = [] for key in teacher_dict: teacher = '' num = 0 for item in teacher_dict[key]: num += 1 number_of_courses.append((key,num)) big_list.append([teacher, number_of_courses])


Now I get a message that my first task is not passing (I had to calculate the total number of teachers there).

Umesh Ravji
Umesh Ravji
42,386 Points

If the engine says a previous task has broken, it's most likely that there is a coding (syntax) error in your code. It helps if you make your code look neat, use the 'markdown cheatsheet' link by the post answer button to take a look at how too.

I'm not sure if you included that loop in your stats method, but here's pretty much the only lines of code you will need to complete this part. Just have to work out how many courses the teacher teachers, and you know how to work out how many items are in a list :)

def stats(teacher_dict):
    big_list = []
    for teacher, courses in teacher_dict.items():
        number_of_courses = 42  # calculate using courses list here
        big_list.append([teacher, number_of_courses])
    return big_list
Ilia Galperin
Ilia Galperin
6,522 Points
def num_teachers(arg):
    s = 0
    for i in arg:
        s += 1
    return s

def num_courses(arg):
    t = 0
    for i in arg:
        for x in arg[i]:
            t += 1
    return t


def courses(teachers):
    output = []
    for courses in teachers.values():
        output +=courses
    return output

def most_courses(dict):
    for key, value in dict.items():
        if max(value):
            return key

def stats(last):
    l = []
    for teacher, courses in last.items():
        tot = 0
        for value in last.values():
            for course in value:
                tot += 1
                l.append([teacher, tot])
    return l
serhiy ivashchenko
serhiy ivashchenko
1,555 Points

def most_courses(dict): list_comp = [[teacher, len(courses)] for teacher, courses in dict.items()] return list_comp