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

any tips on this chalenge?

I'm stuck on the stats function. How do i go about doing this?

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 num_teachers(teacher):
    teacher_count = 0
    for key in teacher.keys():
        teacher_count += 1
    return teacher_count

def num_courses(teacher):
    course_count = 0
    for value in teacher.values():
        for each in value:
            course_count += 1
    return course_count

def courses(teacher):
    courses = []
    for value in teacher.values():
        courses += value
    return courses

def most_courses(teacher_dict):
    max_count = 0
    teacher = None
    for key,val in teacher_dict.items():
        if len(val)> max_count:
            teacher = key
        max_count = len(val)
    return teacher

def stats(teacher):
    teacher_list = []
    inner_list = []
    for key,val in teacher.items():
        inner_list += [key, val]
        teacher_list = teachet_list.append(inner_list)
    return teacher_list

2 Answers

suliman alkous
PLUS
suliman alkous
Courses Plus Student 5,139 Points

ok, in teacher dictionary the key is teacher name and the value is courses so they want the result it will be teacher_name and number of courses so what lan do is half current but except number of courses that it should be count for list (val) this first thing, the second one is "+=" you don't need to use += for inner_item because this mean append to inner_item variable and you don't need to append its a temporary just to hold data. and last thing is this line

teacher_list = teachet_list.append(inner_list)

you don't have to save teacher_list to itself

suliman alkous
PLUS
suliman alkous
Courses Plus Student 5,139 Points

this is current answer

def stats(teacher):
    teacher_list = []
    inner_list = []
    for key,val in teacher.items():
        inner_list = [key, len(val)]
        teacher_list.append(inner_list)
    return teacher_list
Chris Freeman
Chris Freeman
Treehouse Moderator 68,453 Points

We are discouraging simply posting answers in the forum. Please add text on either why the OP code doesn't work or why your solution fixes the problem. Otherwise the answer may be deleted. Thank you.