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

Mattan Yedidya
Mattan Yedidya
1,084 Points

Stats unpacking method

Whats wrong with my stats method? It seems that temp_list is incorrect but I don't know why... Thanks!

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

def num_courses(arg):
    count = 0
    for item in arg:
        count += len(arg[item])
    return count

def courses(arg):
    courses_offered = []
    for item in arg:
        courses_offered += arg[item]

    return courses_offered

def most_courses(arg):
    max_count = 0
    index = ""
    for item in arg:
        if len(arg[item]) > max_count:
            max_count = len(arg[item])
            index = item
    return index     

def stats(arg):
    unpacked = []
    for item in arg:
        temp_list = [item, num_courses(arg)]
        unpacked.extend(temp_list)

    return temp_list

1 Answer

Rich Zimmerman
Rich Zimmerman
24,063 Points

You can unpack dictionaries with the .items() method in a loop.

def stats(arg):
    answer = []
    for key, value in arg.items():
        teacher = [key, len(value)]
        answer.append(teacher)
    return answer