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

Question on Python dictionary

Stuck on the fifth part of this challenge where it's asking me to take a dictionary, and return a two-dimensional list where the inner list has [name, num of courses]. I really thought this code I wrote worked (at the bottom). I even put something similar in my Shell and did the expected result. Can someone help?

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

def num_courses(theDict):
    courses = 0
    for i in theDict.keys():
        for j in theDict[i]:
            courses += 1
    return courses

def courses(theDict):
    newStr = []
    for i in theDict.keys():
        newStr.extend(theDict[i])
    return newStr

def most_courses(theDict):
    max = 0
    teacher = ''
    for i in theDict.keys():
        if max < len(theDict[i]):
            max = len(theDict[i])
            teacher = i
    return teacher

def stats(theDict):
    teacherList = []
    for i theDict.keys():
        teacherList.append([i, len(theDict[i])])
    return teacherList

2 Answers

I notice that your functions are taking in only one dictionary, they should take two (one is for the teachers and one is for the courses). Thus your functions are not working. Read the challenges more closely.

Wait I'm still confused, because the challenge says to take in one argument that is the teacher dictionary. When I do that and try to use a for loop to append the result in a new list, it should complete the challenge no?

In this specific case, I'm referring to def stats(theList)

Part 2 of this challenge asked that you create a function to get the total number of courses; if you can get the stats function to count the number of courses for each teacher then you can create a temporary list in the for loop which is then appended to the outer list. I hope that I make some sort of sense, happy coding mate :)