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

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

need help on task 5 of 5

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(dict):
    return len(dict)
def num_courses(dict):
    cnt=0
    for value in dict.values():
        cnt = cnt+len(value)
    return cnt
def courses(dict):
    courselist=[]
    for value in dict.values():
        for x in value:
            courselist.append(x)
    return courselist
def most_courses(dict):
    values = dict.values()
    keys = dict.keys()
    clist = max(values)

    for key in dict.keys():
        for value in values:
            if value == max(values):
                hold = key

    return hold
def stats(dict):
    keys = dict.keys()
    coursecnt=[]

    for key in dict.keys():
        for value in dict.values():
        #for v in value:
            coursecnt.append(len(value))

    statsdict = list(zip(keys, coursecnt))        
    return statsdict

2 Answers

The challenge says to return a list of lists. Your code returns a list of tuples because zip() returns a list of tuples. You would have to turn each tuple into a list before returning it. For example:

    statsdict = zip(keys, coursecnt) 
    statsdict = [list(item) for item in statsdict]
    return statsdict

Here's a simpler way:

def stats(dict):
    course_list = []
    for key, value in dict.items():
        course_list.append([key, len(value)])
    return course_list

thanks